Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope ActiveRecord queries with OR instead of AND #5545

Closed
emilecantin opened this issue Mar 22, 2012 · 13 comments
Closed

Scope ActiveRecord queries with OR instead of AND #5545

emilecantin opened this issue Mar 22, 2012 · 13 comments

Comments

@emilecantin
Copy link

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
Copy link

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
Copy link
Author

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
Copy link
Contributor

@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.

@kurko
Copy link

kurko commented Mar 27, 2012

For feature requests, please read http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#what-about-feature-requests

This should be closed.

@emilecantin
Copy link
Author

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
Copy link

xxx commented Mar 28, 2012

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
Copy link

kurko commented Mar 28, 2012

@emilecantin email? or dm me.

@emilecantin
Copy link
Author

@kurko emilecantin AT gmail.com

@wakiki
Copy link

wakiki commented Aug 10, 2012

+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?

@fatuhoku
Copy link

@wakiki does your approach support scopes with parameters?

@fatuhoku
Copy link


I found the best solution was to use this monkey patch if you're using Rails <5.

https://gist.github.com/craigweston/d5039765327a07706615

@khiav223577
Copy link

khiav223577 commented Feb 7, 2017

@fatuhoku you may be Interested in this: rails_or, which works in even Rails 3.

@fatuhoku
Copy link

Thanks for the hint!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants