当前位置 : 主页 > 编程语言 > ruby >

ruby-on-rails – Rails:在View中检查has_many

来源:互联网 收集:自由互联 发布时间:2021-06-23
如果我有… class Bunny ActiveRecord::Base has_many :carrotsend …如果@bunny有胡萝卜,我如何查看View?我想做这样的事情: % if @bunny.carrots? % strongYay! Carrots!/strong % for carrot in @bunny.carrots % You got a %
如果我有…

class Bunny < ActiveRecord::Base
  has_many :carrots
end

…如果@bunny有胡萝卜,我如何查看View?我想做这样的事情:

<% if @bunny.carrots? %>
  <strong>Yay! Carrots!</strong>
  <% for carrot in @bunny.carrots %>
    You got a <%=h carrot.color %> carrot!<br />
  <% end %>
<% end %>

我知道@ 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 %>
网友评论