views:

138

answers:

2

Hello all,

I have a DB field that is integer type and values are always 0 or 1. How do I grab the equivalent boolean value in ruby? such that when i do the following the check box is set appropriately:

<%= check_box_tag 'resend', @system_config.resend %>
+2  A: 

You could use the zero? method. It returns true if 0. If you need to backwards, you could easily negate it to !@system_config.resend.zero?. Or you could extend the Fixnum class, adding a to_b? method, since everything is open and extensible in dynamic languages.

class Integer
  def to_b?
    !self.zero?
  end
end

alt text

Ruby API: http://ruby-doc.org/core/classes/Fixnum.html#M001050

Jarrett Meyer
Thanks Jarret. Part of my solution was also to use the check_box_tag helper properly like so:<%= check_box_tag 'resend', @system_config.resend, @system_config.resend %>
rafael
+1  A: 

1 is your only truth value here. So you can get the boolean truth value with number == 1.

Chuck