I have a active record, where I have a usecase: I want to use map
in the ActiveRecord
scopes
. Following is my code:
class SupportedGeoIdAndLocation < ActiveRecord::Base
scope :supported_geo_ids, lambda { all.map(&:geo_id).uniq }
# also tried: scope :supported_geo_ids, lambda { select('distinct geo_id').map(&:geo_id).uniq }
scope :locations_for_geo_id, lambda { |geo_id| where(:geo_id => geo_id).map(&:location) }
end
but when I do SupportedGeoIdAndLocation.supported_geo_ids
I get empty array, while SupportedGeoIdAndLocation.all.map(&:geo_id).uniq
gives me desired result.
I am looking for a way so that this can be done using lambda in one go, not via chaining of different methods and completely avoiding class functions.
Looks like I am using scope in wrong way, Please suggest me what can be correct way.
version: padrino
:0.10.5, ruby
: ruby-1.9.3-p429, ActiveRecord
: 3.1.6