Forum: Ruby on Rails STI and HABTM

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
seth b. (Guest)
on 2006-05-05 08:34
(Received via mailing list)
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
??? Is there a better way to do it?

Haven't done this with STI yet - Thanks.
seth b. (Guest)
on 2006-05-05 09:04
(Received via mailing list)
Moreover, why can't I do a Deal.find or a Product.find ???
Chris T (Guest)
on 2006-05-05 11:45
(Received via mailing list)
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.
seth b. (Guest)
on 2006-05-05 11:57
(Received via mailing list)
They are all in their own separate files - do they need to be all in one

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.
seth b. (Guest)
on 2006-05-05 21:22
(Received via mailing list)
I'm an idiot - I had Product still subclassing ActiveRecord, not Item...

THanks :)
This topic is locked and can not be replied to.