析取中的ActiveRecord [英] Disjunction in ActiveRecord

查看:143
本文介绍了析取中的ActiveRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用ActiveRecord named_scope ■要创建一个查询使用SQL 条款?

Is it possible to use ActiveRecord named_scopes to create one query with sql OR clauses?

当我使用

Model.scope1.scope2

生成的查询是这些领域的配合。

generated query is conjunction of these scopes.

推荐答案

这是不是真的有什么命名范围被设计做的,但你可能会使用他们提供一些额外的code得到你需要的东西。

This isn't really what named scopes were designed to do, but you could probably use them with some additional code to get what you needed.

def combine_scopes(model)
  (model.scope1 + model.scope2).uniq
end

或允许范围来进行组合

or allow any scopes to be combined

def combine_scopes(model, scope1, scope2)
  (model.send(scope1) + model.send(scope2)).uniq
end

你甚至可以改变,要允许任意数量的使用*的args作用域

you could even change that to allow any number of scopes using *args

这篇关于析取中的ActiveRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆