[Feature][ActiveRecord] Find records with present attribute value more easily

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature][ActiveRecord] Find records with present attribute value more easily

Matt Norton
Instead of:
Person.where.not(name: nil)

Use:
Person.present(:name)

Avoids double negative and verbosity.

I'd be happy to contribute if there's interest in this! 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Matt Norton
Alternative:
Person.exists(:name)



On Wednesday, December 5, 2018 at 6:30:12 PM UTC-5, Matt Norton wrote:
Instead of:
Person.where.not(name: nil)

Use:
Person.present(:name)

Avoids double negative and verbosity.

I'd be happy to contribute if there's interest in this! 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Greg Navis
Using "present' might be confusing with the presence validation which is something different (a blank string is not present but is not nil either).

Best regards
Greg Navis

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Tom Rossi
What about using "having"?

Person.having(:name)

On Thu, Dec 6, 2018 at 3:28 AM Greg Navis <[hidden email]> wrote:
Using "present' might be confusing with the presence validation which is something different (a blank string is not present but is not nil either).

Best regards
Greg Navis

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Andrew Kaspick
class ApplicationRecord < ActiveRecord::Base
  scope :not_blank, ->(field) { where.not(field => nil) } # or where.not(field => ['',nil]) depending on your needs
end

And you're done.

As you can see above, what does 'exists' or 'present' mean?  Not nil, or not nil and an empty string or maybe just not an empty string?

On Thu, Dec 6, 2018 at 8:27 AM Tom Rossi <[hidden email]> wrote:
What about using "having"?

Person.having(:name)

On Thu, Dec 6, 2018 at 3:28 AM Greg Navis <[hidden email]> wrote:
Using "present' might be confusing with the presence validation which is something different (a blank string is not present but is not nil either).

Best regards
Greg Navis

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Matt Norton
I like "having". It reads more expressively than "present" and "exists". 

Could provide an option for including/excluding empty strings:

Person.having(:name, empty_strings: true)

By default, should it include or exclude empty strings?

Excluding nil and empty strings is probably more often the desired query. But possibly too magical as a default?

On Thursday, December 6, 2018 at 8:28:13 AM UTC-5, Andrew Kaspick wrote:
class ApplicationRecord < ActiveRecord::Base
  scope :not_blank, ->(field) { where.not(field => nil) } # or where.not(field => ['',nil]) depending on your needs
end

And you're done.

As you can see above, what does 'exists' or 'present' mean?  Not nil, or not nil and an empty string or maybe just not an empty string?

On Thu, Dec 6, 2018 at 8:27 AM Tom Rossi <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">t...@...> wrote:
What about using "having"?

Person.having(:name)

On Thu, Dec 6, 2018 at 3:28 AM Greg Navis <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">con...@...> wrote:
Using "present' might be confusing with the presence validation which is something different (a blank string is not present but is not nil either).

Best regards
Greg Navis

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Kasper Timm Hansen
I don’t think these examples have much sway and the problem itself seems sufficiently solved by `where.not(name: nil)` to me. It’s clearer and does map nicely onto the resulting SQL.

Yes, sometimes Rails does relish syntactic sugar but that’s when it aids clarity.

PS. `having` is already taken: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-having

Den 6. dec. 2018 kl. 15.01 skrev Matt Norton <[hidden email]>:

I like "having". It reads more expressively than "present" and "exists". 

Could provide an option for including/excluding empty strings:

Person.having(:name, empty_strings: true)

By default, should it include or exclude empty strings?

Excluding nil and empty strings is probably more often the desired query. But possibly too magical as a default?

On Thursday, December 6, 2018 at 8:28:13 AM UTC-5, Andrew Kaspick wrote:
class ApplicationRecord < ActiveRecord::Base
  scope :not_blank, ->(field) { where.not(field => nil) } # or where.not(field => ['',nil]) depending on your needs
end

And you're done.

As you can see above, what does 'exists' or 'present' mean?  Not nil, or not nil and an empty string or maybe just not an empty string?

On Thu, Dec 6, 2018 at 8:27 AM Tom Rossi <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" class="">t...@...> wrote:
What about using "having"?

Person.having(:name)

On Thu, Dec 6, 2018 at 3:28 AM Greg Navis <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" class="">con...@...> wrote:
Using "present' might be confusing with the presence validation which is something different (a blank string is not present but is not nil either).

Best regards
Greg Navis

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" class="">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" class="">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/group/rubyonrails-core';return true;" onclick="this.href='https://groups.google.com/group/rubyonrails-core';return true;" class="">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;" class="">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" class="">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZVgIPVPrBgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" class="">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/group/rubyonrails-core';return true;" onclick="this.href='https://groups.google.com/group/rubyonrails-core';return true;" class="">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;" class="">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Filipe W. Lima
In reply to this post by Matt Norton
I personally use `where.not` and find it clear enough as it its.

`.exists` doesn't seem like a good name because it can confused with current `exists?`: https://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-exists-3F

On Wednesday, December 5, 2018 at 10:42:30 PM UTC-3, Matt Norton wrote:
Alternative:
Person.exists(:name)



On Wednesday, December 5, 2018 at 6:30:12 PM UTC-5, Matt Norton wrote:
Instead of:
Person.where.not(name: nil)

Use:
Person.present(:name)

Avoids double negative and verbosity.

I'd be happy to contribute if there's interest in this! 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

JP Duffy
In reply to this post by Tom Rossi
I don't think "having" is a good idea since it already means something else in SQL: https://en.wikipedia.org/wiki/Having_(SQL)


On Thursday, December 6, 2018 at 7:27:20 AM UTC-6, Tom Rossi wrote:
What about using "having"?

Person.having(:name)

On Thu, Dec 6, 2018 at 3:28 AM Greg Navis <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="oxB1_UbrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">con...@...> wrote:
Using "present' might be confusing with the presence validation which is something different (a blank string is not present but is not nil either).

Best regards
Greg Navis

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="oxB1_UbrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="oxB1_UbrBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Anders Lemke
In reply to this post by Matt Norton
I've personally missed the `.present?`-functionality from `Object#present?` for querying my models several times.

So I would argue, that in order to use the word "present" you would have to mimic the behaviour of `Object#present?`.

Then; I would use it.



On Thursday, 6 December 2018 00:30:12 UTC+1, Matt Norton wrote:
Instead of:
Person.where.not(name: nil)

Use:
Person.present(:name)

Avoids double negative and verbosity.

I'd be happy to contribute if there's interest in this! 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

Sachin Mudgal
Personally, if I would ever like to change it, I will go for something like `Person.where(:name).is_not(nil)` or `Person.where(:name).is('Pikachu')` or `Person.where(:name).is_not(like: ['Pikachu', 'Raichu'])` etc.. Otherwise, I feel it already fine enough.

On Fri, Dec 7, 2018 at 2:47 PM Anders Lemke <[hidden email]> wrote:
I've personally missed the `.present?`-functionality from `Object#present?` for querying my models several times.

So I would argue, that in order to use the word "present" you would have to mimic the behaviour of `Object#present?`.

Then; I would use it.



On Thursday, 6 December 2018 00:30:12 UTC+1, Matt Norton wrote:
Instead of:
Person.where.not(name: nil)

Use:
Person.present(:name)

Avoids double negative and verbosity.

I'd be happy to contribute if there's interest in this! 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Find records with present attribute value more easily

DHH-2
In reply to this post by Matt Norton
I agree that a double negative is iffy. But I don't like present on the root. What I do like is: Person.where.present(:name) – so that #present on where becomes a modifier like #not. I would have to have the same semantics as Object#present?, though. So that means that both nil and empty would count. (Also agree that we can't use #having since it already has meaning in SQL).

On Wednesday, December 5, 2018 at 3:30:12 PM UTC-8, Matt Norton wrote:
Instead of:
Person.where.not(name: nil)

Use:
Person.present(:name)

Avoids double negative and verbosity.

I'd be happy to contribute if there's interest in this! 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.