Inheritance not using STI

Hi there,

I have a question about inheritance in Rails.

Say you have 3 tables:

  1. pants
  2. shirts
  3. socks

Each table has different attributes, but they do share some of the same attributes respectively in each table:

Common attributes amongst all the tables:

  1. material_used
  2. color
  3. manufacture

Different attributes:

  1. pants

    • length
    • waist_size
  2. shirts

    • sleeve_length
    • neck_size
  3. socks

    • foot_size
    • is_striped (say this is a boolean value)

Now say someone is browsing through your online clothing store buying socks, shirts and pants and they’re having a great time. When displaying all items in their cart, each line item is of a different product type technically, but yet they all share a few of the same attributes (material_used, color, manufacture).

Would it be wise to create a general Product class that stores the common attributes which is used for the initial @user.products query (assuming you have a valid User object and the superclass Product exists), and then each product type would inherit from Product.

class Product;end # initialize common attributes somehow class Pants < Product;end # specify specific attributes class Shirts < Product;end # specify specific attributes class Socks < Product;end # specify specific attributes

etc…

I guess I’m a little confused on the best way to keep it simple (as well as how to create the Product class, which I think is a proxy class hooked up to ActiveRecord somehow… ?) when iterating through the clothes to see what kind of attributes they have and then display the proper info. I’d like to keep as much business logic out of the view as possible. Obviously Pants don’t have an attribute called “foot_size”. I don’t have the option of changing the database tables too much, so STI is a no-go for this project. I’m thinking more of going the class table inheritance route, but I’m not sure. ( http://www.martinfowler.com/eaaCatalog/classTableInheritance.html).

Are there any tutorials that cover what I’m talking about? If it matters, I’m stuck with Oracle. Did any of this make sense or am I on crazy pills?

Thank you, Dave

Hey,

right now Rails doesn't seem to support CTI. There's a plugin for PostgreSQL, but if you are stuck to Oracle this might not work (http:// clti.rubyforge.org/). As discussed here (http://groups.google.de/group/rubyonrails-talk/ browse_thread/thread/8d4159425d094cd9), the only way is to create a full table for all subclasses and/or set the relations manually. I'm now running in this problem too with a project, so I maybe write later, how I fixed it.

Regards,

Mike

Thanks Jean-Etienne and Mike for the info. I’ll take a look at clti.rubyforge.org. I know it doesn’t work with oracle, but it might be easily extended.

-Dave