sql_values retrieves SQL values using Active Record query interface but without instantiation of AR-objects.
Assuming you've got a class:
class Player < ActiveRecord::Base
validates_presence_of :name
scope :best, order('rating desc')
end
You can now retrieve values
:
Player.values(:name) # => ["boffy", "pitibo", "macovsky"]
Player.best.values(:rating) # => [30, 25, 10]
There's also a shorthand ids
for values(:id)
:
Player.where("rating>?", 10).ids # => [1, 3]
Player.ids(:league_id) # => [5, 3, 4]
And last but not the least, hashes
:
Player.hashes(%w{id name}) # => [{"id" => 1, "name" => "boffy"}, {"id" => 2, "name" => "pitibo"}, {"id" => 3, "name" => "macovsky"}]
That's all folks!