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

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


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.


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=> nil2.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