[Rails] MySQL adapter returns integers as strings?
Scott Barron
scott at elitists.net
Wed Nov 24 13:35:28 GMT 2004
On Wed, Nov 24, 2004 at 09:34:11PM +1100, Gavin Sinclair wrote:
> On Wednesday, November 24, 2004, 8:50:47 PM, David wrote:
>
> >> The integer columns in my DB tables are returned as strings when
> >> ActiveRecord gets involved. This seems annoying and unnecessary to
> >> me. Surely the database adapter knows, or can know, what data types
> >> are used. Is it ActiveRecord's policy to ignore this metadata?
>
> > Active Record does type conversion on real columns where it will have
> > queried the database in advanced for the types (using SHOW FIELDS). It
> > doesn't convert values pulled when you grab the connection yourself nor
> > does it convert piggy-back columns, aggregates, or results from
> > functions.
>
> Hmmmm... I present the following IRB transcript:
>
> >> require 'user'
> >> User.find(1)
> => #<User:0x320f468 @attributes={"name"=>"Jim Jones", "username"=>"jim",
> "id"=>"1", "password"=>"feral", "email"=>"jim at example.com"}>
>
> The ID is a string.
This is how it's printed out in irb, but what does it say when you do:
=> #<User:0x1441a50 @attributes={"name"=>"Scott M", "level"=>"3",
"username"=>"xxx", "id"=>"8", "department_id"=>"2",
"address"=>"xxx at xxx"}>
irb(main):010:0> u.id.class
=> Fixnum
You'll see that irb prints the results with quotes, but it is really a
Fixnum. Does that work for you?
-Scott
More information about the Rails
mailing list