在ActiveRecord中断开
是否可以使用ActiveRecord named_scope
来创建一个带有sql OR
子句的查询?
我用的时候
Model.scope1.scope2
生成的查询是这些范围的结合。
这不是命名范围的设计目的,但您可以将它们与一些额外的代码一起使用以获得所需的内容。
def combine_scopes(model) (model.scope1 + model.scope2).uniq end
或允许任何范围合并
def combine_scopes(model, scope1, scope2) (model.send(scope1) + model.send(scope2)).uniq end
你甚至可以改变它以允许任何数量的范围使用* args
- 通过子对象的最大字段值查找ActiveRecord对象?
- ActiveRecord :: StatementInvalid:Mysql2 :: Error:无法删除或更新父行–Rails 4.2.6
- Rails SQL查询构建器…或ActiveRecord查询构建器
- 如何存储和比较:ActiveRecord中的符号(Ruby on Rails)
- rails + ActiveRecord:缓存模型的所有寄存器
- 如何validationhas_many关系未更改
- 如何在新的has_many关联上使ActiveRecord bump updated_at
- 更好的方法来访问单个Rails ActiveRecord错误?
- 编写ActiveRecord适配器