AR#or_where
Rails 3 中是否有一个
User.where(cond1).or_where(cond2)
?
AR#or_where
Rails 3 中是否有一个
User.where(cond1).or_where(cond2)
?
在 Rails 中没有这样的方法。
我认为在 WHERE 子句中执行 OR 的最简单方法是执行以下操作:
User.where("first_name = ? OR last_name = ?", params[:first_name], "Wilson")
如果你想学习 Arel 方法(它有一个“或”方法),请看这里:https ://github.com/rails/arel#readme 。使用 Arel 和 ActiveRecord,您可以执行以下相同操作:
User.where(User.arel_table[:first_name].eq(params[:first_name]).or(User.arel_table[:last_name].eq('Wilson')))
只需使用User.where(cond1) | User.where(cond2)
, || 由红宝石保留!
你可以看看meta_where。此 gem 允许在 Ruby 代码中使用更强大的 ActiveRecord 查询表达式,而无需删除 SQL 片段。
如果你想像那样把它们锁起来,你必须深入研究一下 arel 。请参阅这个类似的 SO 问题。