views:

67

answers:

5

Dear All, When I use a table from database, if data is not available in table. I will use if and else conditions like

<% if @tables.blank? %>
  <p> Not available</p>
<% else %>
  blaaa blaa
<% end %>

It works fine. Now I need to apply the same thing for connected tables. If data is available in first table tables and not available in benches. How I can apply??

I tried like this

<% @tables.each do |table| %>
  <% if table.benches.blank? %>
    <p> Not available</p>
  <% else %>
    blaaa blaa
  <% end %>
<% end %>

... But its not working. Kindly give me suggestions.

A: 

Try using #empty?.

<% @tables.each do |table| %>
  <% if table.benches.empty? %>
    <p> Not available</p>
  <% else %>
    blaaa blaa
  <% end %>
<% end %>
Justice
`blank?` and `empty?` works similar and in this example should give the same result.
klew
@Justice: "empty" also failed...
Palani Kannan
+2  A: 

I'm not quite sure what you want, but maybe this will help you.

<% if @tables.blank? %>
  <p> Not available</p>
<% else %>
  <% @tables.each do |table| %>
    <% if table.benches.blank? %>
      <p> Not available</p>
    <% else %>
      blaaa blaa
    <% end %>
  <% end %>
<% end %>
klew
@klew: This is the exact code i am using. but it seems to not work.
Palani Kannan
It isn't printing anything? Nor "not availble" nor "blaaa blaa"? Please attach code from controller where you initialize `@table` and code for related models.
klew
Does the code you're using include the `<% if @tables.blank? %>` part? My guess is that what's happening is that `@tables` is blank/empty, which means that the code inside the `each` block is not being executed.
Phil
A: 

What happens if you try this:

<% unless table.benches.exists? %>
  <p> Not available</p>
<% else %>
  blaaa blaa
<% end %>
wschroer
A: 

if/else must output one or the other... make sure you check your HTML source code (not just browser view) to see if the output isn't hiding somewhere inside a tag.

Andrew Vit
A: 

Dear All, Its working... when i apply like this...

<% if @tables.blank? %>
  <p> Not available</p>
<% else %>
  <% @tables.each do |table| %>
    <% table.benches.each do |bench| %>
      <% if bench.column.blank? %>
        <p> Not available</p>
      <% else %>
        blaaa blaa
      <% end %>
    <% end %>
  <% end %>
<% end %>

Thank you for everyone who participated in this Question.

Palani Kannan