Forum: Ruby on Rails What's the best way to do complex inheritance?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Stephen G. (Guest)
on 2006-01-03 21:37
I have a Merchant class that has many Products.  Product is an abstract
class which has many subclasses (ProductA, ProductB, etc).  All Products
have common data, like 'name', 'status', 'created_on', etc... but they
also have some completely different properties.

It seems like Active Record only supports Single Table Inheritance.
This would make my Products' table _huge_ and mostly empty.  Is there a
way around this or am I stuck with a sloppy table?  Would I be better of
just having seperate lists for each Product that the merchant has?
Matt Goss (Guest)
on 2006-01-03 22:00
(Received via mailing list)
Stephen,
Single Table Inheritance will work in this case if you pull out the
extraneous attributes and put them in auxillary tables.  Then you can
associate the appropriate attiribute tables to subclasses of Product as
needed.
JM2C
Matt
Kyle M. (Guest)
on 2006-01-03 22:06
(Received via mailing list)
On 1/3/06, Stephen G. <removed_email_address@domain.invalid> wrote:
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

Product.has_many :attributes

create_table(:attributes) do |t|
  t.column :key, :string
  t.column :value, :text
end

--
Kyle M.
Chief Technologist
E Factor Media // FN Interactive
removed_email_address@domain.invalid
1-866-263-3261
Stephen G. (Guest)
on 2006-01-03 22:38
Matt Goss wrote:
> Stephen,
> Single Table Inheritance will work in this case if you pull out the
> extraneous attributes and put them in auxillary tables.  Then you can
> associate the appropriate attiribute tables to subclasses of Product as
> needed.
> JM2C
> Matt

I tried something like this.  I made a ProductAData and made it belong
to ProductA.  I then created a view so that I could create a new
ProductA with its ProductAData along with it.  If the ProductA fields
are valid, but the ProductAData isn't, the new ProductA is saved to the
database without the ProductAData.  What's the proper way to do this?

BTW, I'm a little green with this stuff (if it wasn't obvious).
This topic is locked and can not be replied to.