Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement in rails

I trying to learn rails by doing some labs in railsforzombies, I am in lab3 (if statements).

It has two tables:

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

Objective In the each block, if a Zombie has more than 1 tweet, print out SMART ZOMBIE

 <% zombies = Zombie.all %>

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

I have tried some solutions but I get it wrong.

like image 937
SHUMA Avatar asked Jan 23 '11 21:01

SHUMA


People also ask

What are conditional statements in Ruby?

Conditionals are formed using a combination of if statements and comparison and logical operators (<, >, <=, >=, ==, != , &&, ||) . They are basic logical structures that are defined with the reserved words if , else , elsif , and end .

Does Ruby have else if?

Notice Ruby uses elsif, not else if nor elif. Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed.

What is the difference between if and unless statement in Ruby?

In if statement, the block executes once the given condition is true, however in unless statement, the block of code executes once the given condition is false.


2 Answers

<% if zombie.tweets.size > 1 %>
  Smart Zombie!
<% end %>
like image 118
Clint Miller Avatar answered Oct 27 '22 01:10

Clint Miller


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

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

Edit: more than 1.

like image 42
scragz Avatar answered Oct 27 '22 00:10

scragz