Forum: Ruby on Rails STI and HABTM

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.
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 seth b. (subimage)
on 2006-05-05 06: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
categories_items
??? Is there a better way to do it?

Haven't done this with STI yet - Thanks.
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 seth b. (subimage)
on 2006-05-05 07:04
(Received via mailing list)
Moreover, why can't I do a Deal.find or a Product.find ???
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-05-05 09: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.
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 seth b. (subimage)
on 2006-05-05 09:57
(Received via mailing list)
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.
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 seth b. (subimage)
on 2006-05-05 19: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.