If you phrase it slightly differently, the code for associations almost
writes itself:
Game has one host to class User
Game has one visitor to class User
User has many hosting games to class Game
User has many visiting games to class Game
By default when you create an association, rails will use the model name
plus "_id" to determine the foreign key. This isn''t what you
want, since
game has 2 foreign keys to user. Adding the class_name option makes it use
the name of the association and the class name plus "_id". In this
case,
host_user_id and visitor_user_id.
So you can define it like this:
class Game
has_one :host, :class_name => User, :inverse_of => :hosting_games
has_one :visitor, :class_name => User, :inverse_of => :visiting_games
end
class User
has_many :hosting_games, :class_name => Game, :inverse_of => :host
has_many :visiting_games, :class_name => Game, :inverse_of => :visitor
end
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.