Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How do I check if a column has a value?

How can I accomplish this?

<% for agent in @broker.agents %>
  ...
  <% if agent.cell %><span class="cell-number">Cell: <%= agent.cell %></span><% end %>
  ...
<% end %>

I want to test to see if the agent has a cell number, and if so, display what's inside the conditional. What I have currently doesn't seem to work; it just displays "Cell: ".

Thoughts?

like image 294
neezer Avatar asked Nov 19 '08 17:11

neezer


3 Answers

I am giving a very detailed answer to this Question "How do I check if a column has a value?".

First of all, it is important to note that an attribute can have four kinds of values in it.

  1. nil value i.e "nil" stored in the database
  2. empty value i.e "" an empty string with no spaces
  3. empty string with spaces " ".
  4. value present in database i.e a non-empty string.

Here is the detail behavior of all the present methods(Ruby 2.2.2) that could be used in this case.

First Method: .empty?

  1. For nil value => Throws an exception

    2.2.2 :037 > object.attribute
    => nil
    2.2.2 :025 > object.attribute.empty?
    NoMethodError: undefined method `empty?' for nil:NilClass
    
  2. For empty value i.e "" an empty string with no spaces

    2.2.2 :037 > object.attribute
    => ""
    2.2.2 :025 > object.attribute.empty?
    true
    
  3. empty string with spaces " ".

    2.2.2 :041 > object.attribute
    => " " 
    2.2.2 :042 > object.attribute.empty?
    => false
    
  4. value present in database i.e a non-empty string.

    2.2.2 :045 > object.attribute
     => "some value" 
    2.2.2 :046 > object.attribute.empty?
     => false 
    

Second Method: .nil?

  1. nil value i.e "nil" stored in the database

    2.2.2 :049 > object.attribute
     => nil 
    2.2.2 :050 > object.attribute.nil?
     => true
    
  2. empty value i.e "" an empty string with no spaces

    2.2.2 :053 > object.attribute
     => "" 
    2.2.2 :054 > object.attribute.nil?
     => false 
    
  3. empty string with spaces " ".

    2.2.2 :057 > object.attribute
     => " " 
    2.2.2 :058 > object.attribute.nil?
     => false 
    
  4. value present in database i.e a non-empty string.

    2.2.2 :061 > object.attribute
     => "some value" 
    2.2.2 :062 > object.attribute.nil?
     => false
    

Third Method: .blank?

  1. nil value i.e "nil" stored in the database

    2.2.2 :065 > object.attribute
     => nil 
    2.2.2 :066 > object.attribute.blank?
     => true
    
  2. empty value i.e "" an empty string with no spaces

    2.2.2 :069 > object.attribute
     => "" 
    2.2.2 :070 > object.attribute.blank?
     => true 
    
  3. empty string with spaces " ".

    2.2.2 :073 > object.attribute
     => " " 
    2.2.2 :074 > object.attribute.blank?
     => true 
    
  4. value present in database i.e a non-empty string.

    2.2.2 :075 > object.attribute
     => "some value" 
    2.2.2 :076 > object.attribute.blank?
     => false 
    

Fourth Method: .present?

  1. nil value i.e "nil" stored in the database

    2.2.2 :088 > object.attribute
     => nil 
    2.2.2 :089 > object.attribute.present?
     => false
    
  2. empty value i.e "" an empty string with no spaces

    2.2.2 :092 > object.attribute
     => "" 
    2.2.2 :093 > object.attribute.present?
     => false
    
  3. empty string with spaces " ".

    2.2.2 :096 > object.attribute
     => " " 
    2.2.2 :097 > object.attribute.present?
     => false 
    
  4. value present in database i.e a non-empty string.

    2.2.2 :100 > object.attribute
     => "some value" 
    2.2.2 :101 > object.attribute.present?
     => true 
    

You can use either of the four depending upon the situation you face.

Thanks

like image 156
techdreams Avatar answered Nov 13 '22 20:11

techdreams


This is what you asked for:

<% for agent in @broker.agents %>
  <% unless agent.cell.blank? %>
    <span class="cell-number">Cell: <%= agent.cell %></span>
  <% end %>
<% end %>

The cell? method works whether cell is nil or an empty string. Rails adds similar functions for all ActiveRecord attributes. This will look a little nicer:

<% for agent in @broker.agents %>
  <span class="cell-number">
    Cell: <%= agent.cell? ? "none given" : agent.cell %>
  </span>
<% end %>

The question mark and colon form a quick "if ? then : else" statement. There are two question marks in the code above because one is part of the method name cell? and the other is a part of the if/then/else construction.

like image 14
Adrian Dunston Avatar answered Nov 13 '22 20:11

Adrian Dunston


if !agent.cell.blank?

It works.

like image 5
neezer Avatar answered Nov 13 '22 19:11

neezer