Jim Burgess
2011-Oct-04 20:35 UTC
Active Record Associations - best way to find all items assosciated with a specific company
I have the following associations in my models: item.rb belongs_to :manufacturer, :class_name => "Company" belongs_to :distributor, :class_name => "Company" company.rb has_many :items My question: what is the best way to find all of the items belonging to a specific company? If I call Company.items I (understandably) get the error message: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: items.company_id: SELECT "items".* FROM "items" WHERE ("items".company_id = ... ) Should I write a little helper method that does what I want, something along the lines of: Item.where("items.manufacturer_id LIKE :record OR items.distributor_id LIKE :record",{:record => @company.id}) or is there a better way to do this through associations? Would be grateful for any advice. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Oct-04 20:40 UTC
Re: Active Record Associations - best way to find all items assosciated with a specific company
On 4 October 2011 21:35, Jim Burgess <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have the following associations in my models: > > item.rb > belongs_to :manufacturer, :class_name => "Company" > belongs_to :distributor, :class_name => "Company" > > company.rb > has_many :items > > My question: what is the best way to find all of the items belonging to > a specific company?If you have a company in @company (for example) then that companies items are @company.items. Similarly for an @item its company is @item.company I think you might benefit from working through some tutorials. railstutorial.org is good and free to use online. I say that assuming that you have already worked through the Rails Guides. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Oct-04 20:44 UTC
Re: Active Record Associations - best way to find all items assosciated with a specific company
On 4 October 2011 21:40, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 4 October 2011 21:35, Jim Burgess <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I have the following associations in my models: >> >> item.rb >> belongs_to :manufacturer, :class_name => "Company" >> belongs_to :distributor, :class_name => "Company" >> >> company.rb >> has_many :items >> >> My question: what is the best way to find all of the items belonging to >> a specific company? > > If you have a company in @company (for example) then that companies > items are @company.items. > Similarly for an @item its company is @item.companyOn second thoughts perhaps *I* would benefit from reading your question more carefully. You have a problem with your associations, for company you need to specify two associations for the two associations to item. I need to just check exactly what you need to do.... Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Oct-04 20:51 UTC
Re: Active Record Associations - best way to find all items assosciated with a specific company
Trying again. On 4 October 2011 21:44, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 4 October 2011 21:40, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 4 October 2011 21:35, Jim Burgess <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> I have the following associations in my models: >>> >>> item.rb >>> belongs_to :manufacturer, :class_name => "Company"I am not sure if it is necessary but you might need :foreign_key => "manufacturer_id" here. I put it in to remind me what the field is called anyway.>>> belongs_to :distributor, :class_name => "Company"Same here>>> >>> company.rb >>> has_many :itemsHere you need two associations has_many :manufacturer_items, :class_name => "Item" has_many :distributor_items, :class_name => "Item" where manufacturer_items is a name you can choose yourself. Then for a particular company @company the items are @company.manufacturer_items and @company.distributor_items. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Burgess
2011-Oct-04 21:07 UTC
Re: Active Record Associations - best way to find all items assosciated with a specific company
Many thanks for your quick help Colin. That''s a nice solution to the problem and it''s also good to find out what the best practice is. Jim -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Oct-04 21:11 UTC
Re: Re: Active Record Associations - best way to find all items assosciated with a specific company
On 4 October 2011 22:07, Jim Burgess <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Many thanks for your quick help Colin. > That''s a nice solution to the problem and it''s also good to find out > what the best practice is.I am not sure I am confident that this is *best* practice, but it works for me. Others more experienced may well have alternative suggestions. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Burgess
2011-Oct-04 21:14 UTC
Re: Re: Active Record Associations - best way to find all items assosciated with a specific company
Although this does indeed work well, I would still be interested to hear any other suggestions ... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.