Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Setting booleans in AR
# When params come back from a form, checked boxes come back as 'field_name' => "on".
# So we can use that to update attributes, right?
model.field_name # => false
model.update_attribute(:field_name, "on")
model.field_name # => false
# WTF?
@scottharvey

This comment has been minimized.

Show comment Hide comment
@scottharvey

scottharvey Jan 12, 2012

These are the only two that seem to work for me

model.update_attribute(:field_name, "1")
model.update_attribute(:field_name, true)

These are the only two that seem to work for me

model.update_attribute(:field_name, "1")
model.update_attribute(:field_name, true)
@alassek

This comment has been minimized.

Show comment Hide comment
@alassek

alassek Jan 12, 2012

The best you could probably do here is overload model#field_name= to convert 'on' to true.

Or it might be possible to monkeypatch AR.

alassek commented Jan 12, 2012

The best you could probably do here is overload model#field_name= to convert 'on' to true.

Or it might be possible to monkeypatch AR.

@radar

This comment has been minimized.

Show comment Hide comment
@radar

radar Jan 12, 2012

Confirm what @scottharvey says, "on" won't work. "1", 1, 0, "0", true, "true", false and "false" are the only values that I think Rails supports. There may also be support for 't' and 'f'.

radar commented Jan 12, 2012

Confirm what @scottharvey says, "on" won't work. "1", 1, 0, "0", true, "true", false and "false" are the only values that I think Rails supports. There may also be support for 't' and 'f'.

@nickhoffman

This comment has been minimized.

Show comment Hide comment
@nickhoffman

nickhoffman Jan 12, 2012

As @scottharvey said, "1" and true are what I've used in the past, though 1 should also work.

Also, "field_name" => "on" seems weird to me. Checkboxes usually result in "field_name" => "1".

As @scottharvey said, "1" and true are what I've used in the past, though 1 should also work.

Also, "field_name" => "on" seems weird to me. Checkboxes usually result in "field_name" => "1".

@clifton

This comment has been minimized.

Show comment Hide comment
@clifton

clifton Jan 12, 2012

These fields, in the mysql[2] adapter are saved as TINYINTs. So if you were doing a query you would say

select * from models where field_name = 1

So, aside from just true and false rails would need to know what other strings to use for truthy/falsey values.

>> "true".true?
=> true
>> "on".true?
=> false

clifton commented Jan 12, 2012

These fields, in the mysql[2] adapter are saved as TINYINTs. So if you were doing a query you would say

select * from models where field_name = 1

So, aside from just true and false rails would need to know what other strings to use for truthy/falsey values.

>> "true".true?
=> true
>> "on".true?
=> false
@clifton

This comment has been minimized.

Show comment Hide comment
@clifton

clifton Jan 12, 2012

Also,

>> model.update_attribute :boolean, "true"
=> true
>> model.boolean
=> true

clifton commented Jan 12, 2012

Also,

>> model.update_attribute :boolean, "true"
=> true
>> model.boolean
=> true
@avdi

This comment has been minimized.

Show comment Hide comment
@avdi

avdi Jan 12, 2012

So it turns out that the jQuery default value for checked boxes is "on".

The list of values ActiveRecord considers "truthy" for setting boolean attributes is: [true, 1, '1', 't', 'T', 'true', 'TRUE']

!@&%.

Owner

avdi commented Jan 12, 2012

So it turns out that the jQuery default value for checked boxes is "on".

The list of values ActiveRecord considers "truthy" for setting boolean attributes is: [true, 1, '1', 't', 'T', 'true', 'TRUE']

!@&%.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment