【发布时间】:2013-05-14 15:47:39
【问题描述】:
我需要一个 ActiveRecord 查询来匹配 params[] 数组中的所有项目。
我当前的方法是根据找到任何标签给出结果,而不是“匹配所有”
class Product < ActiveRecord::Base
has_many :tags
def self.filter_by_params(params)
scoped = self.scoped
scoped = scoped.includes(:tags).where(:tags => {:id => params[:t]}) if params[:t]
scoped
end
end
我试图编写如下所示的内容,但它没有给我任何结果。有人可以让我朝着正确的方向前进吗?有没有办法说" AND "?
def self.filter_by_params(params)
scoped = self.scoped.includes(:tags)
params[:t].each do |t|
scoped = scoped.where(:tags => {:id => t})
end
scoped
end
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 activerecord