在连接中应用范围
我有以下模型:
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, :enabled => true, :out_of_stock => false})
如果可以在联接中应用指定范围,那就太好了:
Category.joins(:offers).where(:items => {:merchant_id => @merchant.id, :scope => :default})
I have following models:
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
I've faced following code duplication, repeating scope conditions across entire project, when using joins:
Category.joins(:offers).where(:items => {:merchant_id => @merchant.id, :enabled => true, :out_of_stock => false})
It would be nice, if applying specified scope in joins will be possible:
Category.joins(:offers).where(:items => {:merchant_id => @merchant.id, :scope => :default})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试像这样使用
&
:我认为这称为插值。它将两个查询连接在一起,保留第一个查询作为基(它返回
Category
对象)。Try using
&
like this:I think this is called interpolation. It joins the two queries together, preserving the first one as the base (it return
Category
objects).