# STI with the type data depending on the association

**URL:** <https://www.ruby-forum.com/t/sti-with-the-type-data-depending-on-the-association/164588>\
**Category:** Rails\
**Created:** [April 2, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/sti-with-the-type-data-depending-on-the-association/164588 "2009-04-02T20:06:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@mhfs](https://www.ruby-forum.com/u/mhfs)\
**Post date:** [April 2, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/sti-with-the-type-data-depending-on-the-association/164588/1 "2009-04-02T20:06:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mhfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@mhfs](https://www.ruby-forum.com/u/mhfs)\
**Post date:** [April 2, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/sti-with-the-type-data-depending-on-the-association/164588/2 "2009-04-02T23:33:28Z")

</div>

On Apr 2, 7:05 pm, Marcelo de Moraes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![mhfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@mhfs](https://www.ruby-forum.com/u/mhfs)\
**Post date:** [April 4, 2009, 12:40am UTC](https://www.ruby-forum.com/t/sti-with-the-type-data-depending-on-the-association/164588/3 "2009-04-04T00:40:09Z")

</div>

On Apr 3, 7:14 pm, Marcelo de Moraes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> coming from an enumeration) and define a method in the supplier model to get  
> its type (is\_vendor?, is\_manufacturer)… KIS[S] 🙂

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

---

<div class="post-metadata">

**Author:** ![mhfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@mhfs](https://www.ruby-forum.com/u/mhfs)\
**Post date:** [April 3, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/sti-with-the-type-data-depending-on-the-association/164588/4 "2009-04-03T20:15:28Z")

</div>

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] 🙂

But of course, any suggestions appreciated!

Thanks,

Marcelo.

On Thu, Apr 2, 2009 at 3:32 PM, Frederick C.  
\<removed\_email\_address@domain.invalid
