I'm trying to find the best solution to filter the objects in a has_many association. What I have for a setup is
class Company < ActiveRecord::Base
class Product < ActiveRecord::Base
Then in my CompaniesController#show method what I'd like to do is filter the products by it's title.
@company = Company.find(params[:id])
# Then I just want the @company.products where title = params[:title]
Any help would be greatly appreciated.
Where is your controller action getting the product title (params[:title]) from? If you're just passing it in as a search term submitted through a form, then you should be able to run something like....
@products = @company.products.where("title = ?", params[:title])
EDIT: Storing the associated products in the @company variable would be problematic because then @company will contain an array of associated products (instead of a Company object), which not only limits the information you can access about the specific company in your show action from that variable, it also becomes confusing (e.g you're using a variable named @company to store a list of products associated to a company)