如果我有...
class Bunny < ActiveRecord::Base has_many :carrots end
...如果@bunny
有任何胡萝卜,我怎么办查看?我想做这样的事情:
<% if @bunny.carrots? %> Yay! Carrots! <% for carrot in @bunny.carrots %> You got a <%=h carrot.color %> carrot!
<% end %> <% end %>
我知道@bunny.carrots?
不行 - 会怎么样?
<% if @bunny.carrots.any? %> Yay! Carrots! <% for carrot in @bunny.carrots %> You got a <%=h carrot.color %> carrot!
<% end %> <% end %>