Marcelo de Moraes Serpa
2009-Apr-02 18:05 UTC
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. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-02 21:32 UTC
Re: STI with the type data depending on the association
On Apr 2, 7:05 pm, Marcelo de Moraes Serpa <celose...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marcelo de Moraes Serpa
2009-Apr-03 18:14 UTC
Re: STI with the type data depending on the association
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 <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Apr 2, 7:05 pm, Marcelo de Moraes Serpa <celose...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-03 22:39 UTC
Re: STI with the type data depending on the association
On Apr 3, 7:14 pm, Marcelo de Moraes Serpa <celose...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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] :) >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> But of course, any suggestions appreciated! > > Thanks, > > Marcelo. > > On Thu, Apr 2, 2009 at 3:32 PM, Frederick Cheung <frederick.che...@gmail.com > > > wrote: > > > On Apr 2, 7:05 pm, Marcelo de Moraes Serpa <celose...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---