Created
August 11, 2011 22:03
-
-
Save ramon/1140913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def filter_by_country | |
funds = Fund.any_in(institution_id: current_account.institutions.map(&:id)) | |
.only(:asset_ids) | |
asset_ids = funds.map(&:asset_ids).flatten.uniq | |
map = <<-MAP | |
function() { | |
country = db.countries.find(this.country_id) | |
emit(country.name, 1) | |
} | |
MAP | |
reduce = <<-REDUCE | |
function(k, values) { | |
var sum = 0; | |
for(var i in values) | |
sum += values[i]; | |
return sum; | |
} | |
REDUCE | |
@assets = Asset.collection.map_reduce(map, reduce, { query: { _id: { in: asset_ids } } } ) | |
# assets = Asset.any_in(_id: asset_ids).order_by([[:country_id, :asc]]) | |
# @assets_by_country = assets.group_by(&:country_name) do |country, assets| | |
# [country, assets.count] | |
# end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment