Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Scope ActiveRecord queries with OR instead of AND #5545

Closed
emilecantin opened this Issue · 9 comments

6 participants

@emilecantin

ActiveRecord should provide a way to chain scopes using OR, as asked here: http://stackoverflow.com/questions/3684311/rails-how-to-chain-scope-queries-with-or-instead-of-and

Reproduced here for your convenience:

I'm using Rails3, ActiveRecord

Just wondering how can I chain the scopes with OR statements rather than AND.

e.g.

Person.where(:name => "John").where(:lastname => "Smith")
That normally returns name = 'John' AND lastname = 'Smith', but I'd like:

name = 'John' OR lastname = 'Smith'

I have some named scopes defined (for a search functionality), and I would like to be able to reuse them for a keyword search.

@bobbytables

where(:name => ["John", "Smith"]) should solve your problem.

Context:

[4] pry(main)> Group.where(:name => ['Vanguard Cadets', 'Santa Clara Vanguard'])
  Group Load (16.0ms)  SELECT "groups".* FROM "groups" WHERE "groups"."name" IN ('Vanguard Cadets', 'Santa Clara Vanguard')
@emilecantin

Thanks, but I think I haven't made myself clear. The above was an example from the internet, not my actual problem. Here it is: I have a Location model like this:

class Location < ActiveRecord::Base
    belongs_to :city

    def self.search_by_city (query)
      joins(:city).merge(City.search(query))
    end

    def self.search_by_description (query)
      where("locations.description LIKE ?", "%#{query}%")
    end

end

With that, I can do Location.search_by_city "Seattle", Location.search_by_description "College", or even Location.search_by_city("Seattle").search_by_description("College"), which ANDs the two conditions. I want to be able to OR them without rewriting them each time. (DRY principle)

@jgaskins

@bobbytables Your solution is great when you're comparing a single field against multiple values. Unfortunately, that's not the issue here. :-)

I've actually wanted something to abstract the SQL away, as well. ActiveRecord leaves a bit too much SQL in the hands of the user.

@emilecantin

Ok, I will close the issue. I am perfectly willing to add the feature myself, but I do not know where to start.

Anyone willing to help me, or at least point me in the right direction?

@xxx

You can do this if you jump down a level into arel. Something like

people = Person.arel_table
Person.where(people[:name].eq('John').or(people[:lastname].eq('Smith')))

I do agree it would be nice to be able to do this without diving into arel however.

@kurko

@emilecantin email? or dm me.

@emilecantin

@kurko emilecantin AT gmail.com

@wakiki

+1 for this.

I wrote a quick attempt here: https://gist.github.com/3312792

though it uses the syntax:
Jobship.includes(:job).or(Jobship.accepted, Jobship.declined).count

whereas it might be better like this:

Jobship.where(accepted.or(declined)).count

Anyone interested in helping me work on this further?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.