Calculate parent and child totals

Asked

Viewed 390 times

5

Friends have 2 templates. Orders have several Details. I use Cocoon to create forms. The order model has a total that is the sum of the child model totals. What I would like to know is what is the best way to implement a sum in the child and then in the father, of the total field and quantity, whenever the model is saved.

order model fields:

t.string   "customer_id"
t.decimal  "valor_total"
t.integer  "item_total"
t.string   "order_num"
t.datetime "created_at"
t.datetime "updated_at"

fields of the Detail model:

t.string   "order_id"
t.string   "cod_produto"
t.string   "desc_produto"
t.string   "cod_cor"
t.string   "desc_cor"
t.string   "desc_tamanho"
t.decimal  "preco"
t.integer  "quantidade"
t.datetime "created_at"
t.datetime "updated_at"
t.float    "total"

order.Rb

class Order < ActiveRecord::Base
   has_many :details, dependent: :destroy
   belongs_to :customer
   accepts_nested_attributes_for :details, :reject_if => :all_blank, :allow_destroy => true
   validates :customer_id,
             :presence => true
end

Detail.Rb

class Detail < ActiveRecord::Base
   belongs_to :order
end

3 answers

2

Personal problem solved. The models got like this:

class Order < ActiveRecord::Base
has_many :details, dependent: :destroy
belongs_to :customer
accepts_nested_attributes_for :details, :reject_if => :all_blank, :allow_destroy => true
validates :customer_id,
        :presence => true
before_save :sum_details
private
  def sum_details
   self.valor_total = self.details.inject(0){|sum,detail| sum +   detail.total }
  end
end

class Detail < ActiveRecord::Base
belongs_to :order
before_save :sum_prices
   private
   def sum_prices
     self.total = self.preco*self.quantidade 
   end
end

Thank you all!

0

Do the following, on the model Detail.Rb, leave it like this:

class Detail < ActiveRecord::Base
   belongs_to :order
   before_save :sum_prices

   private 
   def sum_prices
     self.total = self.preco*self.quantidade
     self.save!
   end

end

No order.Rb leave so:

class Order < ActiveRecord::Base
   has_many :details, dependent: :destroy
   belongs_to :customer
   accepts_nested_attributes_for :details, :reject_if => :all_blank, :allow_destroy => true
   validates :customer_id,
             :presence => true
   before_save :sum_details


   private
   def sum_details
     total = 0
     self.details each do |d|
      total += d.total
     end
     self.valor_total = total
     self.save!
   end
end
  • Mauricio is giving error : Systemstackerror in Orderscontroller#update stack level Too deep. He should not first make the sum of the children and then the father ?

0

To make the sum_details method a little more concise.

def sum_detail
  self.valor_total = self.details.inject(0){|sum,detail| sum + detail.total }
end

Removes self.save! from sum_prices and sum_details.

  • Good idea. But the error mentioned above keeps giving.

  • Vilmar, you could unify 2 other answers only in this one. It doesn’t seem that they are different solutions. If the intention was to comment on the other reply, do not use the reply to comments field. When you reach 50 reputation, you can comment on any question :)

  • To make it a little more concise: self.details.map(&:total). reduce(:+)

Browser other questions tagged

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