I've been playing around with STI and belongs_to / has_many relationships and I'm a bit confused.
I have a few questions based on a model configuration similar to:
class Parental < ActiveRecord::Base
end
class Mother < Parental
has_many :babies
end
class Father < Parental
has_many :babies
end
class Baby < ActiveRecord::Base
belongs_to :??????
end
- What should
Baby
belong_to? - In terms of a migration, what should i name/add for foreign key on
the
babies
table? - I've had a hard time researching this, is there a definitive source that explains this? The API docs did not seem to hit it on the head OR i missed it (which is totally possible).
My first thought is add parental_id
to babies
along with a method like Baby#owner
that does the following:
- Hits self.parental
- Determines the parental's type
- Returns the correct type of parental (could be a mother, could be a father)
Thank you!