Hello, I have a small query, in a rails application, I have seen this a lot:
args[:where] && args[:where][:establishment_id]
The query is that both mean, are they argument hash of ruby? the second looks for "where the key is establishment_id?
Hello, I have a small query, in a rails application, I have seen this a lot:
args[:where] && args[:where][:establishment_id]
The query is that both mean, are they argument hash of ruby? the second looks for "where the key is establishment_id?
I'm assuming they should use it so the code doesn't crash if it's
args[:where]
not present.For example:
then in case en
args
was not defined:where
, the code would not crash.Given the last definition of
args
, if you access directly:establishment_id
without checking that:where
, your code will fail:if what bothers you is seeing so much
args[:where] &&
, another way to achieve the same thing is with.try
:And if you're using ruby 2.3 or higher, you can use
.dig
and make your code even simpler: