OK,題目並不好......AR 3範圍上HBTM關係檢索元素相匹配的所有ID
這裏是例子:
class Product
has_and_belongs_to_many :categories
end
我想創建一個作用域返回包含我作爲參數發送的所有類別ID的產品。如果我使用Product.includes(:categories).where(:「categories.categorie_id」=> [1,2,3,4]),它會爲我提供所有具有1,2,3,4類別的產品。 ,4. 我想產品至少有我發送的所有類別作爲參數。
例如:
Product.with_all_categories([1, 2, 3, 4]) # => get all the Product that have categories 1, 2, 3 AND 4 (at least, it could be more).