Friends I have 2 models. Orders have several details. I use cocoon to create the forms. The order model has a total that is the sum of the totals of the child models. What I would like to know is how best to implement a sum in the child and then in the parent of the total field and quantity whenever the template is saved.
order template 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"
template detail fields:
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