Assigning wrong data type to attribute: how to trap error

A model has only one column, a numeric and the class definition has no
constraints define.:

create_table "examples"do |t|
t.integer “numberid”

in IRB I create a row, save it, and then attempt to assign “abcd” to
the attribute. Clearly this is wrong, and in fact the assignment does
not take place. I would like however to trap this error, which I
cannot seem to do. Example below:

t = Example.create(:numbid => 4)
=> #<Example id: 3, numberid: 4>

t.numberid = “mmmm”
=> “mmmm”

=> 0

=> “mmmm”

Is there a configuration option to make this kind of assignment
“noisy” in some way so I can trap the error and provide a meaningful
message to the user?

Thanks for any help,


you can add validations to your model.

Thanks Sean, I understand that and do it. Just surprised to find that
the actual assignmnent doesn’t fail, and does return the bogus value
as the result of the assignment.

So to error processing: my validation message string is:
“Must be a number between -180 and 180, not

However it appears that the *_before_type_cast methods (indeed any
attribute methods) aren’t available in the scope of validations. Any
ideas how to access these methods in the message string? Or
alternative suggestions on how to achieve the objective of having a
meaningful message including the wrong text?

Regards, --Kip