B
B
Bogdan2017-05-16 11:32:11
Ruby on Rails
Bogdan, 2017-05-16 11:32:11

Filtering and value in Rails?

Hello. Is it possible in Rails, what if the value is not found, then what would return a null value, and not fall into an error?

count = products.find_by( date: '2017-05-16' ).count

Of course, you can do it like this, but maybe there is a more elegant solution?
product = products.find_by( date: '2017-05-16' )
count = product.present? ? product .count : 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur Bordenyuk, 2017-05-16
@HighQuality

Product.where(date: '2017-05-16').count # => 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question