ruby - Rails: Check has_many in View -


if have...

class bunny < activerecord::base   has_many :carrots end 

...how can check in view if @bunny has carrots? want this:

<% if @bunny.carrots? %>   <strong>yay! carrots!</strong>   <% carrot in @bunny.carrots %>     got <%=h carrot.color %> carrot!<br />   <% end %> <% end %> 

i know @bunny.carrots? doesn't work -- would?

<% if @bunny.carrots.any? %>   <strong>yay! carrots!</strong>   <% carrot in @bunny.carrots %>     got <%=h carrot.color %> carrot!<br />   <% end %> <% end %> 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -