nee*_*zer 3 ruby model ruby-on-rails
如果我有...
class Bunny < ActiveRecord::Base
has_many :carrots
end
Run Code Online (Sandbox Code Playgroud)
...如果@bunny有任何胡萝卜,我怎么办查看?我想做这样的事情:
<% if @bunny.carrots? %>
<strong>Yay! Carrots!</strong>
<% for carrot in @bunny.carrots %>
You got a <%=h carrot.color %> carrot!<br />
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我知道@bunny.carrots?不行 - 会怎么样?
<% if @bunny.carrots.any? %>
<strong>Yay! Carrots!</strong>
<% for carrot in @bunny.carrots %>
You got a <%=h carrot.color %> carrot!<br />
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)