I have User, Game and GameView. GameView describe what games users have seen. Trouble is I can’t figure out what conditions should I use to fetch unviewed games.
class User < ActiveRecord::Base
has_many :game_views
has_many :unviewed_games, :through => :game_views, :source => :game, ???what conditions???
end
class GameView < ActiveRecord::Base
belongs_to :user
belongs_to :game
end
I don’t see this as an association because an association is generally something where you have a foreign key pointing to something, but in this case you have no foreign key. I see this as more of an instance attribute which I would do like this:
You could do a
NOT IN (1,2,3)by querying the viewed games, but that can get very inefficient, very very fast. This is one time I would write out the SQL. I would also do one more thing:That will store it in an instance variable for the length of the request, and save you the multiple database hits. You can do
||=, but if somehow you were to get anil, then you would still query the database multiple times. Rails should cache, but call me paranoid.Hope this helps!