ruby-on-railsruby-on-rails-3rails-activerecord

ActiveRecord OR query


How do you do an OR query in Rails 3 ActiveRecord. All the examples I find just have AND queries.

Edit: OR method is available since Rails 5. See ActiveRecord::QueryMethods


Solution

  • Use ARel

    t = Post.arel_table
    
    results = Post.where(
      t[:author].eq("Someone").
      or(t[:title].matches("%something%"))
    )
    

    The resulting 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%'))