ActiveRecord或查询
你如何在Rails 3 ActiveRecord中进行OR查询。 我find的所有例子都只有AND查询。
编辑: OR方法是可用的自Rails 5.请参阅ActiveRecord :: QueryMethods
使用ARel
 t = Post.arel_table results = Post.where( t[:author].eq("Someone"). or(t[:title].matches("%something%")) ) 
由此产生的SQL:
 ree-1.8.7-2010.02 > puts Post.where(t[:author].eq("Someone").or(t[:title].matches("%something%"))).to_sql SELECT "posts".* FROM "posts" WHERE (("posts"."author" = 'Someone' OR "posts"."title" LIKE '%something%')) 
 如果你想在一列的值上使用一个OR运算符,你可以将一个数组传递给。而ActiveRecord将使用IN(value,other_value) : 
 Model.where(:column => ["value", "other_value"] 
输出:
 SELECT `table_name`.* FROM `table_name` WHERE `table_name`.`column` IN ('value', 'other_value') 
 这应该在单个列上实现等同的OR 
在Rails 3中,应该是
 Model.where("column = ? or other_column = ?", value, other_value) 
这也包括原始的SQL,但我不认为有一种方法在ActiveRecord做OR操作。 你的问题不是一个简单的问题。
Rails / ActiveRecord的更新版本可能本机支持这种语法。 它看起来类似于:
 Foo.where(foo: 'bar').or.where(bar: 'bar') 
正如在这个拉请求https://github.com/rails/rails/pull/9052
就目前而言,只要坚持下面的工作很好:
 Foo.where('foo= ? OR bar= ?', 'bar', 'bar') 
  更新:根据https://github.com/rails/rails/pull/16052 orfunction将在Rails 5中可用 
更新:function已被合并到Rails 5分支
Rails最近添加了这个到ActiveRecord。 它看起来将在Rails 5中发布。致力于掌握已经:
https://github.com/rails/rails/commit/9e42cf019f2417473e7dcbfcb885709fa2709f89
 Post.where(column: 'something').or(Post.where(other: 'else')) # => SELECT * FROM posts WHERE (column = 'something') OR (other = 'else) 
如果你想使用数组作为参数,下面的代码在Rails 4中工作:
 query = Order.where(uuid: uuids, id: ids) Order.where(query.where_values.map(&:to_sql).join(" OR ")) #=> Order Load (0.7ms) SELECT "orders".* FROM "orders" WHERE ("orders"."uuid" IN ('5459eed8350e1b472bfee48375034103', '21313213jkads', '43ujrefdk2384us') OR "orders"."id" IN (2, 3, 4)) 
更多信息: 在数组中作为参数的OR查询 。
  Rails 5带有一个or一个方法。  (l 墨水到文件 ) 
 这个方法接受一个ActiveRecord::Relation对象。 例如: 
 User.where(first_name: 'James').or(User.where(last_name: 'Scott')) 
只需在条件中添加一个OR即可
 Model.find(:all, :conditions => ["column = ? OR other_column = ?",value, other_value]) 
MetaWhere插件是完全惊人的。
轻松混合OR和AND,在任何关联上join条件,甚至指定OUTER JOIN!
 Post.where({sharing_level: Post::Sharing[:everyone]} | ({sharing_level: Post::Sharing[:friends]} & {user: {followers: current_user} }).joins(:user.outer => :followers.outer} 
 我只是从客户端工作中提取这个插件 ,让你把范围与.or.结合起来.or.  ,例如。  Post.published.or.authored_by(current_user) 。  Squeel(MetaSearch的更新实现)也很好,但是不会让你的OR范围,所以查询逻辑可以有点冗余。 
你可以这样做:
 Person.where("name = ? OR age = ?", 'Pearl', 24) 
或更优雅,安装rails_orgem,并像这样做:
 Person.where(:name => 'Pearl').or(:age => 24) 
用rails + arel,更清晰的方法:
 # Table name: messages # # sender_id: integer # recipient_id: integer # content: text class Message < ActiveRecord::Base scope :by_participant, ->(user_id) do left = arel_table[:sender_id].eq(user_id) right = arel_table[:recipient_id].eq(user_id) where(Arel::Nodes::Or.new(left, right)) end end 
生产:
 $ Message.by_participant(User.first.id).to_sql => SELECT `messages`.* FROM `messages` WHERE `messages`.`sender_id` = 1 OR `messages`.`recipient_id` = 1 
 Book.where.any_of(Book.where(:author => 'Poe'), Book.where(:author => 'Hemingway')