Open theoryshaw opened 11 years ago
Simple answer: No and No.
belongs_to
and has_one
(and has_many
, etc.) are class methods that define instance level methods (on ActiveRecord models), so that objects of those classes can be accessed along associations (or, in database language, primary key / foreign key relationships can be used). They often appear in "pairs" (e.g. in User
: has_many :projects
and in Project
: belongs_to :user
), but don't have to (if access in only one direction is required). See detailed explanations here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Concerning the association from User
to Collaborator
, has_one :collaborator
would in fact be wrong, as the model Collaborator
forms a many-to-many relationship between User
and Node
, so if access from User
to all related instances of Collaborator
would be required, has_many :collaborators
would be correct; probably the name Collaborator
is misleading, CollaboratingUser
would probably be better.
Hope this helps :)
HI Chris, thanks for the answer... actually didn't mean to have you answer it, I just put this question up for me really, as a question to answer myself in the future, when i have a little more understanding.
I'm continuing to work my way through learning rails... it's slow going, but I feel i'm making progress. :)
Hope all is well with you.
should each
belongs_to
have a correspondinghas_one
orhas_many
in the associated model?should User model have
has_one :collaborator
?