Summing values of a column of a Ruby-related entity

Asked

Viewed 300 times

3

I am trying to build an application in Rails. I have a resource where the user registers products and sales. The relationship is too much for many, to pass an array of products for sales made of the following

def sale_params
    params.require(:sale).permit(:value, :client_id, :installments, product_ids: [])
end

I would like a hint how I do to sum up all product prices. I did so in the view

  <tbody>
    <% @sales.each do |sale| %>
    <tr>
      <td><%= sale.product.sum(:price) %></td>
      <td><%  sale.products.each do |product| %>
       <li> <%= link_to product.name, product_path(product)  %></li>
      <% end %></td>
      <%= sale.products.count %>
      <td><%= sale.client.name %></td>
      <td><%= sale.installments %></td>
      <td><%= link_to 'Show', sale %></td>
      <td><%= link_to 'Edit', edit_sale_path(sale) %></td>
      <td><%= link_to 'Destroy', sale, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
    <% end %>
</tbody>

It worked but it’s not legal to put business rule in the view. Something else wanted that when registering this value was updated as I select the products to add to the sale, I did so, but it doesn’t work.

<%= form_for(@sale) do |f| %>

    <div class="field">
      <%= f.label :value %>
      <%= f.text_field :value, :value => @sale.products.sum(:price) %>
    </div>

   <div class="field">
     <%= f.label :client_id %>
     <%= f.select :client_id, Client.all.collect {|c| [c.name, c.id]}, include_blank: true %>
  </div>

<br/>
    <%= f.label :products %>
    <% for product in Product.all %>
    <div >
      <%= check_box_tag "sale[product_ids][]", product.id, @sale.products.include?(product) %>
     <%= product.name %> -
     <%= product.price %>
   </div>
  <% end %>
<br/>

   <div class="field">
     <%= f.label :installments %>
     <%= f.text_field :installments %>
   </div>


  <div class="row">
    <div class="span1 actions">
       <%= f.submit :class => 'btn btn-primary' %>
    </div>
  </div>
<% end %>

1 answer

1

You can use the relationship itself to sum up the values

sale.products.sum(:price)

To get these querys out of the view you have to learn to use the feilds_for

http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html

http://railscasts.com/episodes/196-nested-model-form-part-1

example

<%= form_for(@sale) do |f| %>
#...

<div class="field">
  <%= f.collection_select(:city_id, options_for_select(Client.all, :id, :name)) %>
</div

  <%= f.fields_for :products do |product| %>
    <%= product.label :product %>
    <div >
    <%= product.collection_check_boxes :product_ids, Product.all, :id, product.name %>
  <% end %>
  • 1

    Complementing the answer, to update the sum of the values of the products, I imagine you want to update as soon as the user selects another product by checkboxes. Therefore, you will have to use Javascript to do this. If you get confused by the distinction between Rails code and Javascript (when to use one, when to use another?), first reflect on where the stop happens. If it is on the server side it is Rails, if it is on the user’s browser it is Javascript. This distinction is very important to work well with javascript, study it well too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.