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

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

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

unit testing - How to mock PreferenceManager in Android? -