# How to assign boolean values?

**URL:** <https://www.ruby-forum.com/t/how-to-assign-boolean-values/184543>\
**Category:** Rails\
**Created:** [March 4, 2010, 5:00am UTC](https://www.ruby-forum.com/t/how-to-assign-boolean-values/184543 "2010-03-04T05:00:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [March 4, 2010, 5:00am UTC](https://www.ruby-forum.com/t/how-to-assign-boolean-values/184543/1 "2010-03-04T05:00:45Z")

</div>

Dear Friends,

I have created a Boolean field like

table.boolean :log\_status, :default =\> 0, :null =\> false

When i try to add log\_status in my controller part, by default the value  
is assigned as 1.

I dont know why it is?

I aslo assigned the value as zero explicitly, like

@log = LogSettings.new  
@log.log\_status = 0

but even after hard coding like this, the log\_status field is added as 1  
only.

could any anyone please suggest on why its so?

In mysql the field is created as

Field Datatype Null Default  
‘log\_status’, ‘tinyint(1)’, ‘NO’, ‘’, ‘0’, ‘’

Thanks in advance

Regards,  
Martin

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [March 4, 2010, 5:59am UTC](https://www.ruby-forum.com/t/how-to-assign-boolean-values/184543/2 "2010-03-04T05:59:50Z")

</div>

Sent from my iPhone

On Mar 3, 2010, at 8:00 PM, dare ruby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dear Friends,
> 
> I have created a Boolean field like
> 
> table.boolean :log\_status, :default =\> 0, :null =\> false

Hi, the above could have been easily implemented as follows:

table.boolean :log\_status, :default =\> false, :null =\> false

> When i try to add log\_status in my controller part, by default the  
> value  
> is assigned as 1.
> 
> I dont know why it is?

> I aslo assigned the value as zero explicitly, like
> 
> @log = LogSettings.new  
> @log.log\_status = 0

I would recommend using true and false within your Ruby code because  
0 is interpreted as a true value condition. Also, when you access  
the column within your Ruby code, the following is recommended approach:

if @log.log\_status?

# do something for the true case

else

# do something for the false case

end

Note: you’ll need to append the ‘?’ onto boolean column’s name when  
accessing it within your code.

Good luck,

-Conrad
