0

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?

1192805
  • 988
  • 2
  • 10
  • 26
  • Why do you want to use `find_by_sql` and alias `id`? – Marek Lipka Jun 23 '13 at 19:28
  • need to union 3 select statements across several tables. alias allows me to determine which ids belong to what models, but then changes them to quoted strings. – 1192805 Jun 23 '13 at 22:55

1 Answers1

0

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)

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63