STI and HABTM


#1

Hey gang,

How does STI work with HABTM?

For instance…let’s say I have the following models

Category < ActiveRecord::Base

Item < ActiveRecord::Base
Product < Item
Deal < Item

Each product or deal can have categories. Would I then setup Item with
has_and_belongs_to_many :categories, then create a table
categories_items
??? Is there a better way to do it?

Haven’t done this with STI yet - Thanks.


#2

Moreover, why can’t I do a Deal.find or a Product.find ???


#3

You should be able to do a Deal.find. I have a very similar setup and
have no prob. Wondering if it’s finding the subclass declarations. Are
the subclasses in the same file: item.rb or in their own ones? Yes,
you’d have the has_and_belongs_to_many :categories declaration in the
item setup, and the subclasses would inherit the relationship.


#4

They are all in their own separate files - do they need to be all in one
file???

If I try Product.find or Deal.find from script/console I get a SQL error
telling me the table products or deals doesn’t exist.


#5

I’m an idiot - I had Product still subclassing ActiveRecord, not Item…

THanks :slight_smile: