Question

A basic ActiveRecord query returns an unquoted id, for example:

User.first.attributes => {"id"=>2, ...

In find_by_sql, the aliased columns are returned with values in quotes, see id in this case:

all = User.find_by_sql("SELECT u.id as u_id from users u").first.attributes => {"u_id"=>"2"}

How can the quoting of values be avoided?

Was it helpful?

Solution

find_by_sql maps returned columns to object fields by name. If you return a different column name in resultset Rails have no way to know what type to return. (so it may map everything to string - wild guess)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top