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
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 ?
– gmadeira