How to assign boolean values?

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

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