-4

how to sum of columns in rails

i have 6 columns i want to sum of that columns data and display in view page.

  t.integer :basic
  t.integer :da
  t.integer :hra
  t.integer :conveyance
  t.integer :spical_allowance
  t.integer :bonus

example

basic: 11124
da: 300
hra: 36
conveyance: 96
spical_allowance: 100

total value = 11656

in view i am displaying all data

<%@users.salaries.each do |s|%>
<%=s.basic%> 
<%=s.da%>
<%=s.hra%>
<%=s.conveyance%>
<%=s.spical_allowance%>
<%=s.bonus%>
<%end%>

but i am get confused to display total value data in view page pls help me

adarsh
  • 306
  • 5
  • 16
  • This questions is fairly broad I think. These are basic features (performing a sum on an `ActiveRecord`, and creating views) of Rails indicating that you may need to have a look at Rails documentation or tutorial. In the Rails Active Record documentation. look for the method called `.sum`. Have you made any attempts and started creating your view? That would help specify your question better. It's not clear what, more specifically, you're having difficulty with. – lurker Oct 12 '19 at 12:17
  • Here's a link to the `.sum` documentation I mentioned: [ActiveRecord sum](https://apidock.com/rails/ActiveRecord/Calculations/sum). Also see this question already asked: [How to show sum of column in Rails](https://stackoverflow.com/questions/58310009/how-to-show-sum-of-column-in-rails). – lurker Oct 12 '19 at 12:25
  • Please explain what you are confused about. I provided the documentation for `.sum` and provided a link to another question that shows a couple of examples how to use it. – lurker Oct 12 '19 at 12:48
  • can you show the `@users` variable code in your controller? –  Oct 12 '19 at 13:22
  • You're not reading the documentation for `sum` and you're not reading the examples carefully. It takes 0 or 1 arguments. You gave it 6 arguments. It only sums one column. If you want the grand total, you can compute that manually by adding up the subtotals for the columns. – lurker Oct 12 '19 at 13:31

1 Answers1

2

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 %>