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@googlegroups.com.
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.

Search Discussions

  • Colin Law at Oct 4, 2011 at 8:41 pm

    On 4 October 2011 21:35, Jim Burgess 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@googlegroups.com.
    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.
  • Colin Law at Oct 4, 2011 at 8:45 pm

    On 4 October 2011 21:40, Colin Law wrote:
    On 4 October 2011 21:35, Jim Burgess 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
    On 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@googlegroups.com.
    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.
  • Colin Law at Oct 4, 2011 at 8:52 pm
    Trying again.
    On 4 October 2011 21:44, Colin Law wrote:
    On 4 October 2011 21:40, Colin Law wrote:
    On 4 October 2011 21:35, Jim Burgess 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 :items
    Here 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@googlegroups.com.
    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.
  • Jim Burgess at Oct 4, 2011 at 9:07 pm
    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@googlegroups.com.
    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.
  • Colin Law at Oct 4, 2011 at 9:12 pm

    On 4 October 2011 22:07, Jim Burgess 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@googlegroups.com.
    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.
  • Jim Burgess at Oct 4, 2011 at 9:14 pm
    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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 4, '11 at 8:36p
activeOct 4, '11 at 9:14p
posts7
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Colin Law: 4 posts Jim Burgess: 3 posts

People

Translate

site design / logo © 2021 Grokbase