OpeningDesign / OpeningDesign_Old

A social, open-source platform to co-design and co-work with fellow architects and engineers....via real-time collaboration tools.
http://www.openingdesign.com/
Other
5 stars 2 forks source link

should each ```belongs_to``` have a corresponding ```has_one``` or ```has_many```? #51

Open theoryshaw opened 11 years ago

theoryshaw commented 11 years ago

should each belongs_to have a corresponding has_one or has_many in the associated model?

should User model have has_one :collaborator?

Chris927 commented 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 :)

theoryshaw commented 11 years ago

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.