In the following instance, what would the difference be between using belongs_to :mother and has_one :mother for the Chlid class? I’ve been reading the Rails documentation on this and I can’t see how either one would make a difference apart from the semantics involved with reading it.
From what I can tell, the various associations add extra methods to each class, but I haven’t been able to find the documentation to list per association what the methods are and what they do.
class BiologicalMother < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :biological_mother
end
In your case the
has_manybelongs_tois the right approach not just semantically but as how rails works. The foreign key is always stored in thebelongs_topart of the association.A valid
has_onescenario could be like having aPurchasemodel whichhas_oneBillingAddress.example:
Regarding your case, you cant use
has_manyin one side andhas_oneat the other side of the association because thebelongs_topart holds the foreign key always.Let me know if this works for you.