我想创建一个具有某些条件的范围,其中返回的不是特定范围。目前,该解决方案有效:
scope :my_scope, ->(my_var) {
scope = where('TRUE')
if my_var.condition1?
scope = scope.where({ :some_condition => :some_value })
end
if my_var.condition2?
scope = scope.where({ :some_condition => :some_value })
end
scope
}
还有其他更好的解决方案来做到这一点吗?
Regards
在 Rails 4 中,您可以简单地使用all
:
scope :my_scope, ->(my_var) {
if my_var.condition?
where(some_condition: :some_value)
else
all
end
}
ㅤ
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)