method
select_values
![Wide documentation Importance_3](https://d2vfyqvduarcvs.cloudfront.net/images/importance_3.png?1349367920)
select_values(arel, 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]
Register or
log in
to add new notes.
tadman -
April 9, 2009
moiristo -
June 17, 2011
moiristo -
June 20, 2011
hosh -
August 25, 2015
![Default_avatar_30](https://www.gravatar.com/avatar/b850a6cd0c0f8993fa152645ca9a7fbd?default=http://apidock.com/images/default_avatar_30.png&size=30)
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]
![Default_avatar_30](https://www.gravatar.com/avatar/67364c084713f82d505a52316d90dabd?default=http://apidock.com/images/default_avatar_30.png&size=30)
0 thanks
select_values returns Strings for postgreSQL
Will return strings too when using postgreSQL and gem pg (0.11.0).
![Default_avatar_30](https://www.gravatar.com/avatar/67364c084713f82d505a52316d90dabd?default=http://apidock.com/images/default_avatar_30.png&size=30)
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
![Default_avatar_30](https://www.gravatar.com/avatar/ee66b348a67eed43e98d1ed8ad25f138?default=http://apidock.com/images/default_avatar_30.png&size=30)
0 thanks
Using Arel
You can also use Arel.
For example:
class ArticlePage < ActiveRecord::Base belongs_to :article scope :published, -> { where.not(published_at: nil) } scope :all_ready, -> { select("every(workflow_state = 'ready') AS is_ready") } end class Article < ActiveRecord::Base has_many :article_pages def all_ready? ActiveRecord::Base.select_values(article_pages.all_ready,published) = 't' end end