0

params[]配列内のすべてのアイテムに一致する ActiveRecord クエリが必要です。

私の現在の方法では、「MATCH ALL」ではなく、タグの検索に基づいて結果が得られます

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
4

2 に答える 2

0

:includes の代わりに :joins を使用する必要があります

http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables

class Product < ActiveRecord::Base
    has_many :tags

    def self.filter_by_params(params)
        scoped = self.scoped
        scoped = scoped.joins(:tags).where(:tags => {:id => params[:t]}) if params[:t]
        scoped
    end
end
于 2013-05-14T20:30:18.437 に答える