Create a scope, perhaps something like this.
scope :mv, select('*,quantity*market_price as market_value, quantity*market_price/sum(quantity*market_price) as percent')
which creates two virtual attributes, market_value and percent. The problem I am facing is creating a percentage with the included amount (). If I add sum (), the region returns a single record.
I need to calculate the percentage of market value relative to the total market value of a set of registered species.
Example:
1, market_value: 100, percent: .10 2, market_value: 100, percent: .10 3, market_value: 100, percent: .10 4, market_value: 100, percent: .10 5, market_value: 100, percent: .10 6, market_value: 500, percent: .50 Total is 1000
however, if I find it in the place where market_value <6, I should see
1, market_value: 100, percent: .20 2, market_value: 100, percent: .20 3, market_value: 100, percent: .20 4, market_value: 100, percent: .20 5, market_value: 100, percent: .20 Total 500
How can i do this?
I created a self.pct method, but the problem with the self.pct method is that it should run after all areas. if repocoded, the solution is wrong
Still
class Position < ActiveRecord::Base attr_accessible :account_id, :account_type, :market_price, :quantity, :report_date, :symbol scope :long_only, where(:account_type => 'L') scope :short_only, where(:account_type=>"S") scope :equity_only, :conditions => ["symbol <> 'USD'"] scope :mv, select('*,quantity*market_price as market_value, quantity*market_price/sum(quantity*market_price) as percent') scope :mv1, lambda{|total| select(total) }