On Wed, Apr 7, 2010 at 9:30 AM, Andy J.
The typecasting for a MySQL attribute to a Ruby class is done in the
read_attribute method in
the activerecord/lib/active_record/attribute_methods.rb file. In that method
you can see they only typecast the value if column_for_attribute returns the
column (i.e. if it’s defined in the schema for the table).
Excellent info, this little tibdit helps me, too, coming to Rails for a
mod_perl world where everything is dynamically typed (“1” == 1 etc). The
typecasting logic of ActiveRecord has been a learning curve.
Anyone know of a more detailed description of how the typecasting
works, from ActiveRecord down to the MySQL level? That’d be a super
document/overview to have. Each layer (app, activerecord, ruby mysql
mysql itself) will have it’s own concerns on this chain of events.
In certain versions of MySQL, the typecasting can become extremely
important, due to strange bugs in MySQL. For instance, if you do
“select * from mytable where id > ‘1234’”
and 1234 is an integer column but you’ve put in quotes (i.e. a string)
the query, and it is outside the bounds of the index on id, MySQL will
resort to a table scan. i saw this bug wreak havoc on a large rig once,
mod_perl world where the database adapter put quotes around integers.
But that’s just an example of how fully understanding typecasting from
top-down can be a really useful thing.