Rails3:合并范围与OR [英] Rails3: combine scope with OR
本文介绍了Rails3:合并范围与OR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要的名字范围结合或运营商... 是这样的:
I need to combine name scope with or operator... Something like:
class Product < ActiveRecord::Base
belongs_to :client
scope :name_a, where("products.name = 'a'")
scope :client_b, joins(:client).where("clients.name = 'b'")
scope :name_a_or_b, name_a.or(client_b)
end
THX
推荐答案
目前还不支持或
运营商。它的工作是这样的:
users.where(网友[:名称] .EQ('鲍勃')或(用户。[:年龄] .LT(25)))
The
OR
operator is not yet supported. It will work like this:users.where(users[:name].eq('bob').or(users[:age].lt(25)))
这RailsCast 展示了如何使用。或
运营商。但是,它与阿雷尔对象的工作,而你有的ActiveRecord ::关联
的实例。
您可以使用一个关系转换为阿雷尔 Product.name_a.arel
,但现在你必须找出如何合并的条件。
This RailsCast shows you how to use the .or
operator. However, it works with Arel objects while you have instances of ActiveRecord::Relation
.
You can convert a relation to Arel using Product.name_a.arel
, but now you have to figure out how to merge the conditions.
这篇关于Rails3:合并范围与OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文