Rails How to Sum Columns

Rails how to sum columns?

You can summarize directly on the database by creating the respective SQL like this:

Pakke.sum(:prismd, :conditions => {:id => [4,5]})

On newer Rails versions (i.e. Rails >= 4.0), you can use an ActiveRecord queries more intuitively, such as:

Pakke.where(id: [4,5]).sum(:prismd)

See ActiveRecord::Calculations for more usage examples and general documentation.

How to Show Sum of Column in Rails

So you are not using @earnings in a proper way, in your view you use earnings as a collecion of objects, but in controller @earnings are defined as a sum of money but you are not using it, if you really need this sum, you can set two instance variables fiest @earnings = Earning.all and the second one @money_sum = Earnings.sum(:money) and then you can display somewhere your sum as = @money_sum, then you can iterate by @earnings to create some linka in your view

how to sum of columns in rails

I think the solution to the answer is very simple. Please correct me If I'm answering in the wrong direction.

I'm assuming you have a User and Salary model, and you are having this:

# In user_model.rb
has_many :salaries

# In salary_model.rb
belongs_to :user

Condition 1: You want to display salary only of one single user

  • In your controller you may have:

    @user = User.find(params[:id])
  • Then in your view you can do:

    <% @user.salaries.each do |s| %>
    <%= s.basic %>
    <%= s.da %>
    <%= s.hra %>
    <%= s.conveyance %>
    <%= s.special_allowance %>
    <%= s.bonus %>
    Total Value: <%= s.basic + s.da + s.hra + s.conveyance + s.special_allowance + s.bonus %>
    <% end %>

Condition 2: You may want to display all the salaries of all users:

  • In your controller you may have:

    @users = User.all
  • Then in your view you can do:

    <%  @users.each do |u| %>
    <% u.salaries.each do |s| %>
    <%= s.basic %>
    <%= s.da %>
    <%= s.hra %>
    <%= s.conveyance %>
    <%= s.special_allowance %>
    <%= s.bonus %>
    Total Value: <%= s.basic + s.da + s.hra + s.conveyance + s.special_allowance + s.bonus %>
    <% end %>
    <% end %>

Rails: Sum column by group

Figured it out.

Controller

def index
@votes = Vote.all
@originalitysum = Vote.select([:widget_id, :originality]).group('widget_id').sum(:originality)
@votecount = Vote.group(:widget_id).count(:originality)
@votes = Vote.select(:widget_id,
"SUM(originality) as originality_sum",
"SUM(interest) as interest_sum",
"SUM(rating) as rating_sum").group(:widget_id).order("rating_sum DESC")
end

The view

<% @votes.group(:burger_id).each do |vote| %>
<tr>
<td><%= vote.widget.name %></td>
<td><%= vote.widget.store %></td>
<td><%= vote.originality_sum %></td>
<td><%= vote.interest_sum %></td>
<td><%= vote.rating_sum %></td>
</tr>
<% end %>

Thanks to this answer in this link, I was able to parse it together.
Group by a column and then get the sum of another column in Rails

The added bonus is that it allowed me to easily sum the other columns as well.

Rails: Sum value of column on a associated table

The simplest possible solution to get sum of costs for all domains for all websites is:

@websites.includes(:domains).map{|website| website.domains.sum(:costs)}.sum

Edit
If you want to get the sum of costs for all domains that you have in the database, you can use (it will generate only one SQL query):

Domain.all.sum(:costs)

Sum on multiple columns with Activerecord

You can use raw SQL if you need to. Something like this to return an object where you'll have to extract the values... I know you specify active record!

Student.select("SUM(students.total_mark) AS total_mark, SUM(students.marks_obtained) AS marks obtained").where(:id=>student_id)

For rails 4.2 (earlier unchecked)

Student.select("SUM(students.total_mark) AS total_mark, SUM(students.marks_obtained) AS marks obtained").where(:id=>student_id)[0]

NB the brackets following the statement. Without it the statement returns an Class::ActiveRecord_Relation, not the AR instance. What's significant about this is that you CANNOT use first on the relation.

....where(:id=>student_id).first #=> PG::GroupingError: ERROR:  column "students.id" must appear in the GROUP BY clause or be used in an aggregate function

Get the sum of an associated column in a Rails model

Add sum score to user:

class User < ActiveRecord::Base
attr_accessible :name
has_many :predictions

def sum_score
predictions.where("fixture_date <= ?", Date.today).sum(:score)
end
end

In view:

<% @user.each do |u| %>
<%= u.name %><%= u.sum_score %>
<% end %>

Calculating the sum of values of a given column

You have to use map function, like this:

invoices.map(&:amount).sum


Related Topics



Leave a reply



Submit