Jos*_*osh 6 ruby ruby-on-rails
我有这个代码:
<% if @states.count > 0 %> # @states is an active record collection
Run Code Online (Sandbox Code Playgroud)
我觉得应该有更好的方法来写这个.
我正在寻找类似的东西:
<% if @states.not_empty? %>
Run Code Online (Sandbox Code Playgroud)
我意识到这是一个微小的变化,但它将是一个受欢迎的清理.
Edd*_*gan 10
你可能想要ActiveRecord 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 %>
Run Code Online (Sandbox Code Playgroud)