Skip to content

Instantly share code, notes, and snippets.

@avdi
Created January 11, 2012 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avdi/1597569 to your computer and use it in GitHub Desktop.
Save avdi/1597569 to your computer and use it in GitHub Desktop.
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?
@clifton
Copy link

clifton commented Jan 12, 2012

Also,

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

@avdi
Copy link
Author

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