ActiveRecord support for where_not #5950

Closed
wants to merge 1 commit into
from
@jacobo

It's incredibly annoying to negate things in Active Record! I can do:

Person.where(:name => "Jacob")

but to negate it, I'd have to use a SQL fragment like this:

Person.where("name <> ? ", "Jacob")

I want support:

Person.where(:name.not => "Jacob")

(see: #5948)

But barring that, let's support:

Person.where_not(:name => "Jacob")
@jeremyf

Also closes #5160

@jeremyf

@jacobo Unfortunatley the tests do not cleanly merge with master. Could you resolve the merge and let me know.

@jacobo

cool, I'm glad to see interest. Will work on this soon.

How do you feel about a "where_like" method being included also?

@jeremyf

@jacobo Get the rebase passing, and keep the pull request to a single focus; If the rails core team merges the "where_not" then by all means pursue the "where_like"

@jeremy
Ruby on Rails member

Perhaps a Model.where with no args should be chainable: Model.where.not field: nil or Model.where.like name: 'John%'

@tinogomes

where should be chainable. I like it!

@amatsuda
Ruby on Rails member

Perhaps a Model.where with no args should be chainable: Model.where.not field: nil or Model.where.like name: 'John%'

I added this feature to my existing "everywhere" gem as a proof of concept. amatsuda/everywhere@v1.0.1...v2.0.0
You can try this syntax just by adding 'everywhere' to your existing Rails 3 or 4 app's Gemfile.

@jeremy
Does this implementation satisfy your spec?

@tenderlove @jonleighton
And of course I'm willing to push this feature into AR core. Any thoughts? Shall I go ahead and send a PR?

@tenderlove
Ruby on Rails member

@amatsuda seems good. I'd rather we have chainable where than monkeypatch symbol! 👍

@jeremy
Ruby on Rails member

@amatsuda Yes, very nice! Clean implementation, too. 👍

@carlosantoniodasilva
Ruby on Rails member

@amatsuda really cool, thanks for that :)

@carlosantoniodasilva
Ruby on Rails member

@amatsuda do you have any thoughts whether we're going to be able add support for this? Thanks

@amatsuda
Ruby on Rails member

@carlosantoniodasilva Yeah, I'm already having a working branch in my local machine. I'll add some tests and documents, then PR within few days.

@nijikon

+1

@carlosantoniodasilva
Ruby on Rails member

@amatsuda alright, great! That'd be a great addition for sure :)

@jonleighton
Ruby on Rails member

Chainable where also gets a 👍 from me

@sikachu
Ruby on Rails member

@amatsuda すごい!

Would you mind finish up the test, doc change, so we can get this bad boy in before Rails 4 release? :D

@kostyantyn

What are you thinking about implementation of #gt, #gte and etc methods which mongoid orm has. As for me, this syntax is better than "where_#{method_name}"

@frodsan

@amatsuda Hi, did you open a pull request with your implementation? Can we close this PR? Thanks.

@claudiob claudiob added a commit to claudiob/rails that referenced this pull request Nov 29, 2012
@amatsuda amatsuda Relation.where with no args can be chained with not, like, and not_like
examples:

  Model.where.not field: nil
  #=> "SELECT * FROM models WHERE field IS NOT NULL

  Model.where.like name: 'Jeremy%'
  #=> "SELECT * FROM models WHERE name LIKE 'Jeremy%'

this feature was originally suggested by Jeremy Kemper rails#5950 (comment)

Closes #5950
bbc4526
@claudiob claudiob added a commit to claudiob/rails that referenced this pull request Nov 29, 2012
@claudiob claudiob Transforms WhereChain from a mixin to a builder
This feature was originally suggested by José Valim rails#8332 (comment)

The original commit fddf9c2 by Akira Matsuda enabled the methods #not, #like,
and #not_like to be chained to a Relation.where with no args.
This commit maintains the same behavior, but requires using #where before
chaining any of the previous methods, which makes chaining clearer.

Closes #5950
be71e1a
@amatsuda amatsuda added a commit to amatsuda/rails that referenced this pull request Nov 29, 2012
@amatsuda amatsuda Relation.where with no args can be chained with not, like, and not_like
examples:

  Model.where.not field: nil
  #=> "SELECT * FROM models WHERE field IS NOT NULL

  Model.where.like name: 'Jeremy%'
  #=> "SELECT * FROM models WHERE name LIKE 'Jeremy%'

this feature was originally suggested by Jeremy Kemper rails#5950 (comment)

Closes #5950
1d4e319
@jeremy
Ruby on Rails member

Moved to #8332

@jeremy jeremy closed this Nov 29, 2012
@amatsuda amatsuda added a commit to amatsuda/rails that referenced this pull request Nov 29, 2012
@amatsuda amatsuda Relation.where with no args can be chained with not, like, and not_like
examples:

  Model.where.not field: nil
  #=> "SELECT * FROM models WHERE field IS NOT NULL

  Model.where.like name: 'Jeremy%'
  #=> "SELECT * FROM models WHERE name LIKE 'Jeremy%'

this feature was originally suggested by Jeremy Kemper rails#5950 (comment)

Closes #5950
de75af7
@Alamoz

What about when mixing WHERE and NOT clauses? Tried chaining them, which doesn't work. Don't see any example of combining/chaining where and where.not anywhere in the documentation.

@robin850
Ruby on Rails member

@Alamoz : What about this example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment