Grant Birchmeier
Grant Birchmeier

Reputation: 18494

Do I need `all` after `where`? Is it redundant?

This might be a silly question. It's certainly pedantic.

Is there any difference between MyModel.where(...) and MyModel.where(...).all?

This question was prompted by a deprecation warning I got a few minutes ago:

DEPRECATION WARNING: This dynamic method is deprecated.
Please use e.g. Post.where(...).all instead.

Is that all really necessary?

Upvotes: 0

Views: 41

Answers (1)

ABMagil
ABMagil

Reputation: 5655

No, they are equivalent, at least in the way you're probably thinking:

Model.where(id: nil) ===    Model.where(id: nil).all # returns true
Model.where(id: nil) ==     Model.where(id: nil).all # returns true
Model.where(id: nil).eql?   Model.where(id: nil).all # returns false
Model.where(id: nil).equal? Model.where(id: nil).all # returns false

What these equalities tell us (borrowing heavily from this exceptional answer):

  1. Case Equality- sometimes thought of as "If I have a box labeled a, does b get put in it?
  2. "Equality" in the usual thinking. Are these two things the "same", can I replace one with the other without any impacts, do they return the same thing with the same input
  3. .eql? and .equal? being false confirms that these are two different objects. Both that their hashes are non-identical and their object identities are non-identical

Upvotes: 2

Related Questions