0
How to cast boolean values manually
Creat
02/12/2011 17:39:48
per
Roger Campos
We know each database handles boolean values on its own way. Fortunatelly enough ActiveRecord provides an abstraction layer that handles this for us, but sometimes we want to known in a POST action if the given parameters are true or not before actually save the model.
This is a simple method hidding in ActiveRecord internals to do that:
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
This is useful for example if we want to make a virtual boolean attribute, defining this in the model:
def bool_attr=(value)
@bool_attr = ActiveRecord::ConnectionAdapters::Column.value_to_boolean value
end
And using it in the form as a normal attribute:
= f.check_box :bool_attr