我正在尝试找到筛选has_many关联中的对象的最佳解决方案。我有一个设置是通过属性值筛选has_many
class Company < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
attr_accessible :title
belongs_to :company
end
然后在我的CompaniesController#show方法就是我想要做的是过滤产品通过它的标题。
def show
@company = Company.find(params[:id])
# Then I just want the @company.products where title = params[:title]
end
任何帮助将不胜感激。
“Filterrific”轨道宝石是精确的工具这个。该文档有一个关于构建ActiveRecord作用域以过滤has_many关联属性的页面:http://filterrific.clearcove.ca/pages/active_record_scope_patterns.html#filter_by_existence_has_many – 2013-05-17 21:49:21