-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Comments
Context:
|
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:
With that, I can do |
@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. |
For feature requests, please read http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#what-about-feature-requests This should be closed. |
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? |
You can do this if you jump down a level into arel. Something like
I do agree it would be nice to be able to do this without diving into arel however. |
@emilecantin email? or dm me. |
@kurko emilecantin AT gmail.com |
+1 for this. I wrote a quick attempt here: https://gist.github.com/3312792 though it uses the syntax: whereas it might be better like this: Jobship.where(accepted.or(declined)).count Anyone interested in helping me work on this further? |
@wakiki does your approach support scopes with parameters? |
I found the best solution was to use this monkey patch if you're using Rails <5. |
Thanks for the hint! |
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 have some named scopes defined (for a search functionality), and I would like to be able to reuse them for a keyword search.
The text was updated successfully, but these errors were encountered: