[英]How can i filter results in the view using includes in the controller in rails 3?
I have this in my controller: 我的控制器中有这个:
class User::ResourcesController < User::UserController
def index
@resource_type = ResourceType.find_by_name(params[:resource_type].to_s.titleize)
@products = @products.includes(:resources).where(:resources => { :resource_type_id => @resource_type.id })
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @resources }
end
end
end
I am trying to get my resources to be filtered so in my view i can use the code below and have it only pull the resources that have the correct resource_type_id. 我正在尝试过滤我的资源,因此在我看来,我可以使用下面的代码,并仅将具有正确resource_type_id的资源提取出来。
@products.each do |product|
product.resources.count
end
@products = Product.includes(:resources).where("resources.resource_type_id = ?", @resource_type.id)
Try something like this: 尝试这样的事情:
@products = Product.includes(:resources).where(resources: {
resource_type_id: @resource_type.id
})
It's safest and easier to read. 它是最安全且更容易阅读的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.