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?

有帮助吗?

解决方案

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)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top