Flowdock
select_values(sql, name = nil) public

Returns an array of the values of the first column in a select:

select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]
Show source
Register or log in to add new notes.
April 9, 2009
1 thank

select_values returns Strings for MySQL

This method will return all values as strings from MySQL. It is easy to convert if required, for example, to integers:

select_values("SELECT id FROM companies LIMIT 3") => ['1','2','3']
select_values("SELECT id FROM companies LIMIT 3").collect(&:to_i) => [1,2,3]
June 17, 2011
0 thanks

select_values returns Strings for postgreSQL

Will return strings too when using postgreSQL and gem pg (0.11.0).

June 20, 2011
0 thanks

Typecasting return values

A better way to typecast the result array is to use AR’s typecasting capabilities. Example:

column = Company.columns_hash['id']
select_values("SELECT id FROM companies LIMIT 3").map do |value|
  column.type_cast(value)
end