Search code examples
ruby-on-railsruby-on-rails-4model-view-controllermodelnomethoderror

class method defined in model not showing up in controller


rails version 4.2.6

I am attempting to create a checkbox component in a view for a Rails app using HAML.

Relevant view code:

= form_tag movies_path, :method => :get do
  Include:
  - @all_ratings.each do |rating|
    = rating
    = check_box_tag "ratings[#{rating}]"
  = submit_tag 'Refresh'

The controller method, 'index', that renders this view is supposed to create the instance variable "@all_ratings" which is simply an enumerable collection of all possible movie ratings (["G", "PG", "PG-13", "R"]).

Relevant controller code:

def index
    @movies = Movie.order(params[:sort_by])
    @sort_column = params[:sort_by]
    @all_ratings = Movie.all_ratings
end

The method "all_ratings" is a class method of the "Movie" model that I created with:

class Movie < ActiveRecord::Base
    attr_accessible :title, :rating, :description, :release_date

    def self.all_ratings
        Movie.select(:rating).uniq.map { |movie| movie.rating }.sort
    end
end

No matter what I try I continue to get the error:

NoMethodError in MoviesController#index
undefined method `all_ratings' for #<Class:0x000000047bcab0>

I have researched several similar errors here and they generally seem to be related to making a class method vs. instance method mistakes. However, none of the remedies that have worked for these people have worked for me thus far. It seems like no changes I make in the model are ever accessible to the controller.

Thanks very much.


Solution

  • I finally figured it out. I had two files called "movie.rb" in separate projects and I was editing the wrong one. I feel like this is the equivalent of taking your tv apart to fix it and finding out you had just forgotten to plug it in. :|