Saving boolean attributes

Hi,

I’m just learning Ruby on Rails and ran into something where I know I’m
either doing something very stupid or I’m missing something.

My model as a boolean attribute called active_flag. In my Sql Server
database this is stored as a bit value. I then setup a method in my
controller called “deactivate”. This is very simple, it does:

def deactivate
@department = Department.find(params[:id])
@department.active_flag = false
@department.save!
redirect_to :action => ‘list’
end

Since I’m using save!, I’m getting the error:

Validation failed: Active flag can’t be blank

Anybody know why the active_flag field is getting this error when I’m
clearly setting it to false?

Thanks, Mike

On Fri, 2006-06-30 at 22:34 +0200, Mike Witt wrote:

@department = Department.find(params[:id])

clearly setting it to false?


database? is the column in your database actually boolean or numeric
(integer)?

Craig

Craig W. wrote:

On Fri, 2006-06-30 at 22:34 +0200, Mike Witt wrote:

@department = Department.find(params[:id])

clearly setting it to false?


database? is the column in your database actually boolean or numeric
(integer)?

Craig

It’s numeric (SQL Server’s bit type).

On Thu, 2006-07-06 at 03:30 +0200, Mike Witt wrote:

It’s numeric (SQL Server’s bit type).


if it’s a numeric type, then it would seem that the only way to handle
it is to save/search for 0/1

Craig

Craig W. wrote:

On Thu, 2006-07-06 at 03:30 +0200, Mike Witt wrote:

It’s numeric (SQL Server’s bit type).


if it’s a numeric type, then it would seem that the only way to handle
it is to save/search for 0/1

Craig

Then I’m wondering why the scaffolding handles updates and inserts of
this without a problem. I thought of making this column a numeric type
and then wrapping it with a boolean accessor, but I’m guessing there is
a way to make it work with the boolean attribute.

You see, in my scenario, I’m just modifying the scaffold’s destroy
action to be a deactivate action. Since the update action can handle
this, I think I should be able to make the deactivate action handle it
as well.

Thanks for your help, Mike

On Thu, 2006-07-06 at 03:55 +0200, Mike Witt wrote:

this without a problem. I thought of making this column a numeric type
and then wrapping it with a boolean accessor, but I’m guessing there is
a way to make it work with the boolean attribute.

You see, in my scenario, I’m just modifying the scaffold’s destroy
action to be a deactivate action. Since the update action can handle
this, I think I should be able to make the deactivate action handle it
as well.


I’ve lost track of the issue and I don’t use the forum so I don’t
remember the context of the question.

You may want to repeat it.

Craig

Craig W. wrote:

On Thu, 2006-07-06 at 03:55 +0200, Mike Witt wrote:

this without a problem. I thought of making this column a numeric type
and then wrapping it with a boolean accessor, but I’m guessing there is
a way to make it work with the boolean attribute.

You see, in my scenario, I’m just modifying the scaffold’s destroy
action to be a deactivate action. Since the update action can handle
this, I think I should be able to make the deactivate action handle it
as well.


I’ve lost track of the issue and I don’t use the forum so I don’t
remember the context of the question.

You may want to repeat it.

Craig

The original issue:

Hi,

I’m just learning Ruby on Rails and ran into something where I know I’m
either doing something very stupid or I’m missing something.

My model as a boolean attribute called active_flag. In my Sql Server
database this is stored as a bit value. I then setup a method in my
controller called “deactivate”. This is very simple, it does:

def deactivate
@department = Department.find(params[:id])
@department.active_flag = false
@department.save!
redirect_to :action => ‘list’
end

Since I’m using save!, I’m getting the error:

Validation failed: Active flag can’t be blank

Anybody know why the active_flag field is getting this error when I’m
clearly setting it to false?

Thanks, Mike

Craig W. wrote:

What happens when you change

@department.active_flag = false
to
@department.active_flag = 0

Craig

Same error.

On Thu, 2006-07-06 at 04:28 +0200, Mike Witt wrote:


@department.save!
redirect_to :action => 'list'

end

Since I’m using save!, I’m getting the error:

Validation failed: Active flag can’t be blank

Anybody know why the active_flag field is getting this error when I’m
clearly setting it to false?


What happens when you change

@department.active_flag = false
to
@department.active_flag = 0

Craig

Mike Witt wrote:

Then I’m wondering why the scaffolding handles updates and inserts of
this without a problem. I thought of making this column a numeric type
and then wrapping it with a boolean accessor, but I’m guessing there is
a way to make it work with the boolean attribute.
I take this back … scaffolding does not handle the update. I think
the insert is only handled because I have a default value on the column.

On Thu, 2006-07-06 at 14:39 +0200, Mike Witt wrote:

Same error.


suggestion…just before @department.save!

try setting session[:department] = @department

and look at the session variables when you get the error to see what
value is in @department.active_flag

Craig

On 7/6/06, Craig W. [email protected] wrote:

Same error.


suggestion…just before @department.save!

try setting session[:department] = @department

and look at the session variables when you get the error to see what
value is in @department.active_flag

Craig

Sounds like a logic error in your validations, false is a weird bugger
in ruby so you have to be very careful about it. Try looking at the
validations you have and look for places where you’re using x == false
and try x === false or some variation of that.

IIRC, in ruby false is only false when it is the number 0 or the
constant false, so it could be something like the database
misunderstanding and deleting the value or something obscure like
that.

In addition to craigs idea you might try running the breakpointer
right before the save! and just looking at the values as well as
trying to change the var and save manually. Breakpointer rocks
mightily.

Cheers,
Chuck V.

Chuck V. wrote:

On 7/6/06, Craig W. [email protected] wrote:

Same error.


suggestion…just before @department.save!

try setting session[:department] = @department

and look at the session variables when you get the error to see what
value is in @department.active_flag

Craig

Sounds like a logic error in your validations, false is a weird bugger
in ruby so you have to be very careful about it. Try looking at the
validations you have and look for places where you’re using x == false
and try x === false or some variation of that.

IIRC, in ruby false is only false when it is the number 0 or the
constant false, so it could be something like the database
misunderstanding and deleting the value or something obscure like
that.

In addition to craigs idea you might try running the breakpointer
right before the save! and just looking at the values as well as
trying to change the var and save manually. Breakpointer rocks
mightily.

Cheers,
Chuck V.

I’ve used the logger multiple times examining the active_flag attribute
and I’m pretty sure it really is false.

If I remove the validation for :active_flag, then I get a database error
because it tries to update it to null, and nulls are not allowed for
this column.

Thanks, Mike

Chuck V. wrote:

To correct myself, 0 is not a boolean false in ruby. I’m not sure if
it is in rails but I doubt it. The only thing that’s false in ruby is
the constant false.

Ruby also treats nil as false.

regards

Justin

On 7/6/06, Chuck V. [email protected] wrote:

Craig
Craig

In addition to craigs idea you might try running the breakpointer
right before the save! and just looking at the values as well as
trying to change the var and save manually. Breakpointer rocks
mightily.

Cheers,
Chuck V.

To correct myself, 0 is not a boolean false in ruby. I’m not sure if
it is in rails but I doubt it. The only thing that’s false in ruby is
the constant false.

-Chuck