I have this code:
<% if @states.count > 0 %> # @states is an active record collection
I just feel like there should be a better way to write this.
I am looking for something like:
<% if @states.not_empty? %>
I realize this is tiny change but it would be a welcome cleanup.
You probably want ActiveRecord's any?
http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-any-3F
<% if @states.any? %>
Do stuff here if @states has at least one result
<% end %>
How about
<% unless @states.empty? %>
http://apidock.com/rails/ActiveRecord/Base/exists%3F/class
if @states.exists?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With