ROR 3 defining the relation of foreign keys

I am having trouble finding a good tutorial on how to identify the basic foreign key relationships between models. Suppose I have a user model and a game model.

I would like to define two fields in the Game model: host_id and visitor_id, which map to the relation of the foreign key to the user model. The identifiers of the β€œhost” and β€œvisitor” fields of the β€œGame” class basically determine the two players who will participate in the peer-to-peer game ... and these fields must be mapped to the actual users of the application, as defined in the User model by the user ID.

Thanks!

+4
source share
1 answer

Did it try http://guides.rubyonrails.org/association_basics.html ? I think it could be like this:

class User belongs_to :game end class Game has_one :host_user, :class_name => "User" has_one :visit_user, :class_name => "User" end 
+2
source

All Articles