I'm using single table inheritance in my app and I've run into a dilemma on how to best instantiate a subclass. Say we have a class Dwelling
, and subclasses Apartment
and House
. Now Dwelling
is associated with another class Neighborhood
.
models/dwelling
belongs_to :neighborhood
models/neighborhood
has_many :dwellings
Let's say I have instantiated a new neighborhood as follows and I want to create a House
n = Neighborhood.new
Which of these is the correct method?
n.dwellings.build
House.new
If I instantiate using the first method I must fill in the dwelling :type
. But with the second method I must enter the foreign key. I also read that I should never instantiate a subclass via its parent class so that would immediately rule out n.dwellings.build
if this advice was correct? Any guidance would be greatly appreciated, thanks!