Reputation: 1807
I am working on a tutorial and I wanted to really learn by deviating from the tutorial a bit and am encountering some problems. I am creating a showtime app that lists all the movies, times, genres, etc.
I have a Movie model with the attributes name, genre, showtime, showdate, release year, and title. In my movie.rb file, I added an attr_accessor :genre so I can use the setter and getter methods, but when I go into rails console and create a movie with all the attributes, the :genre keeps coming up as "nil" but when I call the movie.genre, it comes up as the correct listed genre. I am not sure why that is?
Also, since genre is not a class, are there any methods I can call on it to list all the genres in the database?
Thank you!
Upvotes: 0
Views: 631
Reputation: 3697
Is genre a column in your movies table? If so, why use attr_accessor?
For the second part of your question, you could write a class method in movie.rb to list all genres. Something like:
def self.genres
Movie.all.map(&:genre).uniq
end
Upvotes: 2