Has anyone ever had problems with :through relationship? I have set it properly, according to the book, but getting response from the browser "Could not find the association :line_items in model Product" and the relationship set in Product model is: has_many :orders, :through => :line_items Order model looks like: has_many :line_items and LineItem belongs_to :product belongs_to :order I want to find orders which contain product with an id = 3 I am doing it through a method def who_bought @product = Product.find(params[:id]) @orders = @product.orders end and then I have an rxml template to iterate over the orders and print them onto the screen. Help please! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
michau wrote:> Has anyone ever had problems with :through relationship? > I have set it properly, according to the book, but getting response > from the browser > > "Could not find the association :line_items in model Product" > > and the relationship set in Product model is: > has_many :orders, :through => :line_items > > Order model looks like: > has_many :line_items > > and LineItem > belongs_to :product > belongs_to :order > > > I want to find orders which contain product with an id = 3 > I am doing it through a method > > def who_bought > @product = Product.find(params[:id]) > @orders = @product.orders > end > > and then I have an rxml template to iterate over the orders and print > them onto the screen. > Help please!The error message "Could not find the association :line_items in model Product" is pretty clear. You haven''t defined the primary line_items association that you need to go through. Do this: Product: has_many :line_items, :dependent => :destroy has_many :orders, :through => :line_items Order: has_many :line_items, :dependent => :destroy has_many :products, :through => :line_items LineItem belongs_to :product belongs_to :order -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Wow, thanks, that helped. Im totally new in RoR and as much as it''s all features seems to be helpful, they''re also confusing sometimes. Cheers On 5 Lip, 14:52, Josh Susser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> michau wrote: > > Has anyone ever had problems with :through relationship? > > I have set it properly, according to the book, but getting response > > from the browser > > > "Could not find the association :line_items in model Product" > > > and the relationship set in Product model is: > > has_many :orders, :through => :line_items > > > Order model looks like: > > has_many :line_items > > > and LineItem > > belongs_to :product > > belongs_to :order > > > I want to find orders which contain product with an id = 3 > > I am doing it through a method > > > def who_bought > > @product = Product.find(params[:id]) > > @orders = @product.orders > > end > > > and then I have an rxml template to iterate over the orders and print > > them onto the screen. > > Help please! > > The error message "Could not find the association :line_items in model > Product" is pretty clear. You haven''t defined the primary line_items > association that you need to go through. Do this: > > Product: > has_many :line_items, :dependent => :destroy > has_many :orders, :through => :line_items > > Order: > has_many :line_items, :dependent => :destroy > has_many :products, :through => :line_items > > LineItem > belongs_to :product > belongs_to :order > > -- > Josh Susserhttp://blog.hasmanythrough.com > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---