Ruby on rails 3 在联接中应用作用域
我有以下型号:Ruby on rails 3 在联接中应用作用域,ruby-on-rails-3,join,finder,Ruby On Rails 3,Join,Finder,我有以下型号: class Category < ActiveRecord::Base has_many :items default_scope where(:enabled => true, :out_of_stock => false) scope :enabled, where(:enabled => true) scope :out_of_stock, where(:out_of_stock => true) end class Item
class Category < ActiveRecord::Base
has_many :items
default_scope where(:enabled => true, :out_of_stock => false)
scope :enabled, where(:enabled => true)
scope :out_of_stock, where(:out_of_stock => true)
end
class Item < ActiveRecord:Base
belongs_to :category
end
如果可以在联接中应用指定的作用域,那就太好了:
Category.joins(:offers).where(:items => {:merchant_id => @merchant.id, :scope => :default})
试着像这样使用
&
:
Category.joins(:offers, :items) & Item.default.where(:merchant_id => @merchant.id)
我想这叫做插值。它将两个查询连接在一起,保留第一个查询作为基础(它返回Category
objects)
Category.joins(:offers, :items) & Item.default.where(:merchant_id => @merchant.id)