tonypm
2008-May-30 13:15 UTC
Help with ActiveRecord for Models with long associtation chain
Hi, I am trying to use this set of associations for what must be a fairly standard data model, to give me the accessor @batch.products Batch: has_many :orders has_many :line_items :through=>orders Order: has_many :line_items belongs_to :batch has_many :products, :through=>:line_items LineItem: belongs_to :product belongs_to :order Product:: has_many :line_items OK I think they are the main associations involved. I sort of want to say that Batch has_many :products, :through=>orders, :through=>line_items This doesnt work and I have tried other variations. The only way i can think at the moment is to make products into a named scope using an sql find, but I am sure there must be a way of telling AR how to do the joins without using sql. TRANSACTION question? I want to get to @batch.products so that I can iterate through each product and update the product stock quantities based on the line_item quantities. Then mark the batch as updated and finally commit as a single batch save. My thinking being that this would be carried out as a safe transaction. The alternative, would be to build a specific transaction, but I am not sure how to do this across 2 models (ie Batch and Product) All help much appreciated. Tonypm --~--~---------~--~----~------------~-------~--~----~ 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
2008-May-30 13:25 UTC
Re: Help with ActiveRecord for Models with long associtation chain
On May 30, 2:15 pm, tonypm <tonypmar...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> Hi, > > OK I think they are the main associations involved. > > I sort of want to say that Batch > has_many :products, :through=>orders, :through=>line_items > > This doesnt work and I have tried other variations. The only way i > can think at the moment is to make products into a named scope using > an sql find, but I am sure there must be a way of telling AR how to do > the joins without using sql. >has_many :through can''t be nested (I''ve vague feeling there''s a plugin that does this). You could probably simplify the find down by using :joins (ie something like Product.find :all, :joins => {:line_items => {:order => :batch}}, :conditions => {''batch.id'' => some_batch_id> TRANSACTION question? > I want to get to @batch.products so that I can iterate through each > product and update the product stock quantities based on the line_item > quantities. Then mark the batch as updated and finally commit as a > single batch save. My thinking being that this would be carried out > as a safe transaction. The alternative, would be to build a specific > transaction, but I am not sure how to do this across 2 models (ie > Batch and Product)Unless batch and product are in different databases, Batch.transaction and Product.transaction are exactly the same. If you don''t like ''preferring'' one over the other you could always do ActiveRecord::Base.transaction do ... end Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
tonypm
2008-May-30 13:54 UTC
Re: Help with ActiveRecord for Models with long associtation chain
Fred, thanks for very quick response. That clarifies things and helps me move forward. Actually my update is complicated further, since a line_item can be a packet with a bill of materials of several products so transaction will be the way to go. and I see I dont need to worry about tables since the transaction is on the actual connection not the database. Many Thanks Tonypm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
tonypm
2008-May-30 14:03 UTC
Re: Help with ActiveRecord for Models with long associtation chain
Fred - Oh and by the way! Product.find :all, :joins=>{:line_items=>{:order => :batch}}, :conditions=>{''batches.id''=>361} worked perfectly - wouldnt have figured that out myself! Thanks Tonypm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---