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?
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.
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…
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.