If the statement is in rails

I am trying to study rails while doing some lab work in railsforzombies, I am in lab3 (if statements).

It has two tables:

  • Zombies {id, name, cemetery}
  • Tweets {id, status, zombie_id}

Target In each block, if Zombie has more than 1 tweet, print SMART ZOMBIE

<% zombies = Zombie.all %> <ul> <% zombies.each do |zombie| %> <li> <%= zombie.name %> # add if statement here </li> <% end %> </ul> 

I tried some solutions, but I'm wrong.

+6
ruby-on-rails
source share
8 answers
 <% if zombie.tweets.size > 1 %> Smart Zombie! <% end %> 
+8
source share

<%= 'SMART ZOMBIE' if zombie.tweets.size > 1 %>

Note: count / length / size are all subtly different .

Edit: more than 1.

+6
source share

It works!!

 <% zombies = Zombie.all %> <ul> <% zombies.each do |zombie| %> <li> <%= zombie.name %> <%= 'SMART ZOMBIE' if zombie.tweets.count > 1 %> </li> <% end %> </ul> 
+5
source share

This should work:

 <ul> <% zombies.each do |zombie| %> <li> <%= link_to zombie.name, edit_zombie_path(zombie) %> <%= 'Smart Zombie' if zombie.tweets.count > 1 %> </li> <% end %> </ul> 
+1
source share

I could do the following work in turn and transmit, I would like to hear if this is bad or not.


In each block, if Zombie has more than 1 tweet, print SMART ZOMBIE

 <% zombies = Zombie.all %> <ul> <% zombies.each do |zombie| %> <li> <%= zombie.name %> if zombie > 1 tweet put "SMART ZOMBIE" </li> <% end %> </ul> 
+1
source share
 <ul> <% zombies.each do |zombie| %> <li> <%= zombie.name %> <% if zombie.tweets.size > 1 %> SMART ZOMBIE <% end %> </li> <% end %> </ul> 
+1
source share
 <% if zombie.tweets.count > 1 %> <em> SMART ZOMBIE </em> <% end %> 

It worked for me

0
source share

I tried, it works!

 <ul> <% zombies.each do |zombie| %> <li> <%= zombie.name %> <% if zombie.tweets.length > 1 %> SMART ZOMBIE <% end %> </li> <% end %> </ul> 
0
source share

All Articles