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.

On 4 October 2011 21:35, Jim B. [email protected] wrote:

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

On 4 October 2011 21:40, Colin L. [email protected] wrote:

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

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

On 4 October 2011 22:07, Jim B. [email protected] 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

Trying again.

On 4 October 2011 21:44, Colin L. [email protected] wrote:

On 4 October 2011 21:40, Colin L. [email protected] wrote:

On 4 October 2011 21:35, Jim B. [email protected] 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

Although this does indeed work well, I would still be interested to hear
any other suggestions …