Rails 4 Sum by Model Method

Rails 4 Sum by Model Method

On Rails 4.1

If goal_ydt is a column in the users table:

@users.sum(:goal_ydt)

If goal_ydt is a method in User class:

@users.to_a.sum(&:goal_ydt)

Sum by model method and inheritance

I think the problem is your use of number_with_precision which is a helper method to use in views which formats your number so that it can be displayed. It returns a string. Rails also provides the sum method which adds all the things in the array together.

Essentially you're getting an array ["12.20", "10.00"] and then it is getting "summed" by doing "12.20" + "10.00"

I would try and keep your total commission as a number as long as possible and only format it using number_with_precision when you are displaying it.

If your getTotalCommission method in ApplicantComission is just:

def getTotalCommission
total.round(2)
end

then your summing code will work as expecting.

P.S. I'd question whether you even need to round it at that point too - you may only need to round it when you output the value

P.P.S. You're not actually doing inheritance which is when different classes inherit from each other. You're methods both have the same interface because they both have a getTotalCommission method but it is not through inheritance.

Rails 4 how to calculate sum for each method

find_by_column always returns only one record.

You can use where condition for multiple ids like this

Model.where(column_name: [array_of_ids])

if view_context.time_plus returns an array of ids

Service.where(price_id: view_context.time_plus]).sum(:time)

ActiveRecord sum

You probably want this:

current_user.items
.select('items.*, items.price * items.sold AS total')
.all

Or, if you want the totals and nothing else:

current_user.items.pluck('items.price * items.sold')

Rails 4, Sum value for nested attribute

If I followed your associations right, something like this.

class Parent
has_many :students
has_many :classinfos, through: :students
has_many :classlists, through: :classinfos

def balance
self.classlists.sum(:tuition)
end
end

Note: support for this style of deeply nested association was added in Rails 3.1, so earlier versions will need a different solution.

Rails sum related records in model

Thats because you have many children(s). And each one of them has many events.

def total_children_hours
hours = 0
self.children(s).each do |children|
hours += children.events.map{|e| e.hours}.to_i.reduce(:+)
end
hours
end

Check this similar thread:

Ruby on Rails nil can't be coerced into BigDecimal

ActiveRecord::Calculations

http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html

Method that sums up all values from references model rails

If you meant to Sum of all tasks based on point for each group If you are trying to get the sum of points for all tasks of each group, then you can do

Task.includes(:group).where("state = ?", 'done').sum(:point)

ActiveRecord: Sum the count of relations

I'm adding the answer that engineersmnky pointed to in the comments on the question, as this was exactly what I'm after.

Add this association to the Manufacturer model:

has_many :widgets, through: :models

There's more information in this article: "Nested Associations and Has_many :through, :source".



Related Topics



Leave a reply



Submit