I’m comfortable using has_many and belongs_to for 1:M relationships.
But how do you implement 0:1 and 0:M relationships? Thanks!
Taylor S. wrote:
I’m comfortable using has_many and belongs_to for 1:M relationships.
But how do you implement 0:1 and 0:M relationships? Thanks!
What does that mean?
Maybe a better question is, “Can Active Record use :has_many and
:belongs to without FK definitions being present in the database?”
For example, I have buildings who have landlords. However, since this
data is input by renters, landlord data will often not be submitted. So
i need to indicate that builds belong to landlords but AR complains when
there is no landlord to attribute it to. If I remove the foreign key
constraint how do I define the relationship?
Taylor S. wrote:
Maybe a better question is, “Can Active Record use :has_many and
:belongs to without FK definitions being present in the database?”For example, I have buildings who have landlords. However, since this
data is input by renters, landlord data will often not be submitted. So
i need to indicate that builds belong to landlords but AR complains when
there is no landlord to attribute it to. If I remove the foreign key
constraint how do I define the relationship?
You’re certainly allowed to have a :belongs_to relationship to nil.
i.e., create a building such that building.landlord_id=nil.
Just don’t “validates_presence_of” on landlord_id.
Jake