Rich Morin wrote:
OK, I understand that, but why (aside documentation and/or
implementation details) does the relationship have to be
expressed in both models? That is, does "belongs_to B" in
A imply "has_one A" in B?
Its all to do with implementation details (which I know you said "aside
from implementation details"), not of the associations, but of dynamic
The model classes aren't loaded into memory when your rails app is
launched, instead they are loaded when you first ask for them (Dr Nic's
Magic Models extends this idea by creating the model class even if it
doesn't exist in your application).
So, if you had Person and Membership classes, where Person.has_many
:memberships; and Membership.belongs_to :person; it is theoretically
possible for the Person.has_many class method to invoke
Membership.belongs_to :person automatically for you.
But, what if you access the Membership class first? Due to the dynamic
loading, if the Person class hadn't been loaded yet, it wouldn't have
generated the has_many and belongs_to associations for us. Our
Membership class would be ignorant of its relationships with other
Inner Rails workings are fun, eh?