STI with the type data depending on the association

Hello list,

I have the following entities:
Product, Supplier. Manufacturer < Supplier and Vendor < Supplier.

Product has_many suppliers through products_suppliers

The thing is, the type of the supplier will depend on the product –
this is
the requirement. So, that’s why I though the type information should be
in
the products_suppliers entity.

What I would like to happen is, when a supplier is retrieved using the
following command:

@suppliers = @product.suppliers.all

Somehow hook up AR and look into the type attribute of the
products_suppliers association and for each record, instantiate the
correct
class (either Vendor or Manufacturer).

It’s STI, only that the type data is in another realm and is “variable”
(depending on the product).

Is there a way to do that with the current STI implementation in Rails
2.2.2? If not, how could I hook up AR to make it happen? Or do you
suggest
something simpler?

Any suggestions appreciated.

Marcelo.

On Apr 2, 7:05 pm, Marcelo de Moraes S. [email protected]
wrote:

What I would like to happen is, when a supplier is retrieved using the
following command:

@suppliers = @product.suppliers.all

Somehow hook up AR and look into the type attribute of the
products_suppliers association and for each record, instantiate the correct
class (either Vendor or Manufacturer).

So for any given row in product_suppliers, there’s a supplier_id.
Surely the row in the suppliers table with that id is either a
Manufacturor or a Vendor ?

Fred

On Apr 3, 7:14 pm, Marcelo de Moraes S. [email protected]
wrote:

coming from an enumeration) and define a method in the supplier model to get
its type (is_vendor?, is_manufacturer)… KIS[S] :slight_smile:

It wouldn’t really be STI - it’s a pretty basic that a given row in
the database (ie object) has one type - feels like you;d be jumping
through hoops to make AR believe you’re doing STI. You might actually
be able to sort of make this work if you did
has_many :suppliers, :through => :product_suppliers, :select =>
‘suppliers.*, product_suppliers.type’ but you might run into odd
sideeffects.

Fred

Hi Fred, thanks for the reply,

So for any given row in product_suppliers, there’s a supplier_id.

Surely the row in the suppliers table with that id is either a
Manufacturor or a Vendor ?

The relationship is like that:

class ProductsSupplier < ActiveRecord::Base
belongs_to :product
belongs_to :supplier
end

class Supplier < ActiveRecord::Base
has_many :product_suppliers
has_many :products, through => :product_suppliers
end

The first though I had was to use STI but somehow make AR
instantiate
the object based on a type attribute in a different model/table (in this
case in the products_suppliers table) so as to make the type vary based
on
the relationship between product and supplier (as a a supplier might be
a
vendor for a product and/or maybe a manufacturer).

The thing is, I tried to monkeypatch AR to add this support to STI, but
the
thing is that I ended up by loosing too much time. I think that I will
just
keep a type field in products_suppliers and save a string value there
(maybe
coming from an enumeration) and define a method in the supplier model to
get
its type (is_vendor?, is_manufacturer)… KIS[S] :slight_smile:

But of course, any suggestions appreciated!

Thanks,

Marcelo.

On Thu, Apr 2, 2009 at 3:32 PM, Frederick C.
<[email protected]