Question

I am trying to set up a structure with a parent that has some attributes and children models that inherit those attributes as well as maintain their own.

Ideally I'd like a setup of

class Parent
  attr_accessible :some_attribute, some_attribute2, some_attribute3
end

class Child < Parent
  attr_accessible :some_child_attr, :some_other_child_attr
end

class OtherChild < Parent
  attr_accessible :something, :something_else
end

In that I can access the columns through the children but also have specific children data attached to the given model. I've been looking at STI and Polymorphic Associations but I want to avoid putting in has_many for every type of child. Is this possible?

Was it helpful?

Solution 2

I have been able to solve my problem using the https://github.com/hzamani/acts_as_relation gem. It seems what I was looking for was called Multi Table Inheritance. Once I got that setup, I was able to create children models with the same attributes as the parent as well as their own individual ones without having to specify every child as a polymorphic association in the parent.

OTHER TIPS

Of course you can. That's how inheritance works. That's the beauty of OOP.

Just put 'has_many' in Parent class and all children have this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top