You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran into unexpected behavior based on the implementation of .empty? as defined in lib/mongoid/findable.rb:
# Returns true if count is zero## @example Are there no saved documents for this model?# Person.empty?## @return [ true | false ] If the collection is empty.defempty?count == 0end
For an example, say you have this class:
classCatincludeMongoid::Documentend
I am now able to call Cat.empty? which executes a query against mongodb to check the collection size.
When this functionality was introduced in mongodb many years ago someone made a similar observation. It feels very non-obvious that calling present? on a class would make a query to a database. Thoughts on deprecating this functionality?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I ran into unexpected behavior based on the implementation of
.empty?
as defined in lib/mongoid/findable.rb:For an example, say you have this class:
I am now able to call
Cat.empty?
which executes a query against mongodb to check the collection size.Consider now how ActiveSupport defines the
present?
method:And now the
blank?
method:Calling
Cat.present?
ultimately callsCat.empty?
and queries mongodb.Contrast this with a relational database-backed ActiveRecord that does not define
empty?
at the class level:When this functionality was introduced in mongodb many years ago someone made a similar observation. It feels very non-obvious that calling
present?
on a class would make a query to a database. Thoughts on deprecating this functionality?Thank you for your time and consideration.
Beta Was this translation helpful? Give feedback.
All reactions