Using a boolean attribute with a conditional statement

I'm betting this is a schoolboy error:

I have a 'protected_profile' boolean attribute in my user model, and I want to use it in a controller action, like so;

if @user.protected_profile = true   some.task else   some_other.task end

But even if the protected_profile boolean returns false in the browser (<%= @user.protected_profile %>), the above action doesn't seem to work.

I barely remember reading something about Ruby assigning true values in most instances - is that what is causing my problems?

I'm betting this is a schoolboy error:

I have a 'protected_profile' boolean attribute in my user model, and I want to use it in a controller action, like so;

if @user.protected_profile = true some.task else some_other.task end

But even if the protected_profile boolean returns false in the browser (<%= @user.protected_profile %>), the above action doesn't seem to
work.

I barely remember reading something about Ruby assigning true values
in most instances - is that what is causing my problems?

No. Your problem is that = is the assignment operator, not the
comparison operator (that's ==) You probably just want: if @user.protected_profile   some.task else   some_other.task end

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

end

But even if the protected_profile boolean returns false in the
browser (<%= @user.protected_profile %>), the above action doesn't seem to work.

I barely remember reading something about Ruby assigning true values in most instances - is that what is causing my problems?

No. Your problem is that = is the assignment operator, not the comparison operator (that's ==) You probably just want: if @user.protected_profile some.task else some_other.task end

Fred

Thanks Fred. I tried your suggestion, but it doesn't work. A user with protected_profile 'false' will still do some.task, rather than some_other.task. The same goes when using the comparison operator, @user.protected_profile == true.

Are you setting protecting profile to the string 'false'? The two are
completely different (and you may not expect it, but the string
'false' is in fact true.

Fred

Neil Cauldwell wrote:

Thanks Fred. I tried your suggestion, but it doesn't work. A user with protected_profile 'false' will still do some.task, rather than some_other.task. The same goes when using the comparison operator, @user.protected_profile == true.

If this is an ActiveRecord model with a *database* type of boolean, you shouldn't assume anything about the actual value of the attribute. Different database adapters will use different values. As Fred says, because the value *displays* as 'false' doesn't mean it is the "false" boolean value.

The best way to test a boolean attribute is with a query predicate:

if @user.protected_profile?

This allows the adapter to translate the database value into a boolean for you...

Thanks guys. The following seems to do the trick;

    if @user.protected_profile? == true