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 Cheung <[email protected]
> wrote:

>
>
>
> On Apr 2, 7:05 pm, Marcelo de Moraes Serpa <[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
>
>
>
> > 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.
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to