Let''s say I have the following models: User has_many :orders has_many :items, :through => :orders Order belongs_to :user has_many :items has_many :products, :through => :items Item belongs_to :order belongs_to :product Product has_many :items has_many :orders, :through => items Is it possible to do: @user = User.find(1) @products = @user.products I have tried but it ddin''t work. If it is impossible to do, what would be the solution to that? -- 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 -~----------~----~----~----~------~----~------~--~---
If all you need to do is get a list of all products ordered by a given user, you could write the following method class User << ActiveRecord::Base def products Product.find :all, :include => [:items], :conditions => ["items.user_id = ? and products.id = items.product_id", self.id] end end Best, kb On Apr 26, 5:26 pm, Fernando Perez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Let''s say I have the following models: > > User > has_many :ordersOn Apr 26, 5:26 pm, Fernando Perez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Let''s say I have the following models: > > User > has_many :orders > has_many :items, :through => :orders > > Order > belongs_to :user > has_many :items > has_many :products, :through => :items > > Item > belongs_to :order > belongs_to :product > > Product > has_many :items > has_many :orders, :through => items > > Is it possible to do: > @user = User.find(1) > @products = @user.products > > I have tried but it ddin''t work. If it is impossible to do, what would > be the solution to that? > -- > 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 -~----------~----~----~----~------~----~------~--~---
Therefore this means that :through can only work through one model. Thank you Kyle for your tip. -- 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 -~----------~----~----~----~------~----~------~--~---
On Apr 26, 5:26 pm, Fernando Perez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Let''s say I have the following models: > (...) > Is it possible to do: > @user = User.find(1) > @products = @user.products > > I have tried but it ddin''t work. If it is impossible to do, what would > be the solution to that?Try this: http://code.torchbox.com/svn/rails/plugins/nested_has_many_through/ See the README for more info. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---