Estoy intentando calcular la cantidad de un campo en especifico, pero cuando envío el formulario, este no se actualiza, tengo que darle editar nuevamente y enviarlo para que funcione, esto me pasa cuando elimino los elementos existentes en la compra.
Son atributos accepts_nested_attributes_for
ItemCompra
del modelo Compra
Modelo Compra
class Compra < ActiveRecord::Base
has_many :item_compras, :dependent => :destroy
accepts_nested_attributes_for :item_compras,
reject_if: RejectDeeplyNested.blank?,
:allow_destroy => true
before_save :calcula_total_bidones
private
def calcula_total_bidones
self.total_bidones = item_compras.map(&:cantidad_bidon).sum
end
end
El modelo ItemCompra
mantiene una relación belongs_to
Simplemente es calcular el total_bidones
(compra) del valor cantidad_bidon(item_compra)
creados a partir de la orden de compra, puede ser uno, como pueden ser 10 o más.
EDICIÓN: Al actualizar los valores ingresados con anterioridad y actualizar el total_bidones
no presenta problema, pero al eliminar los elementos item_compra
desde el formulario de edición de compra
, no se actualiza el total_biones
, si ya no existen elementos, este debería quedar en 0.
EDICIÓN N° 2: He agregado los logs de la consola, que muestra el registro Active Record.
Al crear una nueva compra con elementos item_compra
Started POST "/compras" for ::1 at 2017-07-17 06:09:14 -0400
Processing by ComprasController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pXBg2czUb5ZPGYM+izALlsLLXLa2koP0AizLSU6kP5aI3GgMtPV5n8mwwuUQP9k5qMUcys32tz1zyyuihLjlzQ==", "compra"=>{"total_bidones"=>"", "item_compras_attributes"=>{"0"=>{"cantidad_bidon"=>"10", "_destroy"=>"false"}, "1"=>{"cantidad_bidon"=>"15", "_destroy"=>"false"}, "2"=>{"cantidad_bidon"=>"20", "_destroy"=>"false"}}}, "commit"=>"Create Compra"}
(1.0ms) begin transaction
SQL (2.0ms) INSERT INTO "compras" ("total_bidones", "created_at", "updated_at") VALUES (?, ?, ?) [["total_bidones", 45], ["created_at", "2017-07-17 10:09:14.273013"], ["updated_at", "2017-07-17 10:09:14.273013"]]
SQL (0.0ms) INSERT INTO "item_compras" ("cantidad_bidon", "compra_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["cantidad_bidon", 10], ["compra_id", 3], ["created_at", "2017-07-17 10:09:14.281014"], ["updated_at", "2017-07-17 10:09:14.281014"]]
SQL (0.0ms) INSERT INTO "item_compras" ("cantidad_bidon", "compra_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["cantidad_bidon", 15], ["compra_id", 3], ["created_at", "2017-07-17 10:09:14.287014"], ["updated_at", "2017-07-17 10:09:14.287014"]]
SQL (0.0ms) INSERT INTO "item_compras" ("cantidad_bidon", "compra_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["cantidad_bidon", 20], ["compra_id", 3], ["created_at", "2017-07-17 10:09:14.293014"], ["updated_at", "2017-07-17 10:09:14.293014"]]
(5.0ms) commit transaction
Redirected to http://localhost:3000/compras/3
Completed 302 Found in 53ms (ActiveRecord: 8.0ms)
Log al agregar nuevos item_compra a la compra existente
Started PATCH "/compras/3" for ::1 at 2017-07-17 06:10:01 -0400
Processing by ComprasController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pvSqcYNc0fC7nXvHyOyOKM1rSdlWeeiQS73JbkXXnVOLWKKk+33H+T00OhxT41yHp2UJpS0d3Fk6WimFj8tHCA==", "compra"=>{"total_bidones"=>"45", "item_compras_attributes"=>{"0"=>{"cantidad_bidon"=>"10", "_destroy"=>"false", "id"=>"8"}, "1"=>{"cantidad_bidon"=>"15", "_destroy"=>"false", "id"=>"9"}, "2"=>{"cantidad_bidon"=>"20", "_destroy"=>"false", "id"=>"10"}, "1500286195695"=>{"cantidad_bidon"=>"80", "_destroy"=>"false"}}}, "commit"=>"Update Compra", "id"=>"3"}
Compra Load (1.0ms) SELECT "compras".* FROM "compras" WHERE "compras"."id" = ? LIMIT 1 [["id", 3]]
(0.0ms) begin transaction
ItemCompra Load (1.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_compras"."compra_id" = ? AND "item_compras"."id" IN (8, 9, 10) [["compra_id", 3]]
ItemCompra Load (1.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_compras"."compra_id" = ? [["compra_id", 3]]
SQL (2.0ms) UPDATE "compras" SET "total_bidones" = ?, "updated_at" = ? WHERE "compras"."id" = ? [["total_bidones", 125], ["updated_at", "2017-07-17 10:10:01.372707"], ["id", 3]]
SQL (1.0ms) INSERT INTO "item_compras" ("cantidad_bidon", "compra_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["cantidad_bidon", 80], ["compra_id", 3], ["created_at", "2017-07-17 10:10:01.398709"], ["updated_at", "2017-07-17 10:10:01.398709"]]
(7.0ms) commit transaction
Redirected to http://localhost:3000/compras/3
Completed 302 Found in 215ms (ActiveRecord: 13.0ms)
Log al eliminar todos los item_compra de la compra
Started PATCH "/compras/3" for ::1 at 2017-07-17 06:11:26 -0400
Processing by ComprasController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Arq47vVZo19a51sE7MHmCzDtUPL45mmfzcbLZ4H509EvFrA7jXi1VtxOGt93zjSkWuMQjoOCXVa8ISuMS+UJig==", "compra"=>{"total_bidones"=>"125", "item_compras_attributes"=>{"0"=>{"cantidad_bidon"=>"10", "_destroy"=>"1", "id"=>"8"}, "1"=>{"cantidad_bidon"=>"15", "_destroy"=>"1", "id"=>"9"}, "2"=>{"cantidad_bidon"=>"20", "_destroy"=>"1", "id"=>"10"}, "3"=>{"cantidad_bidon"=>"80", "_destroy"=>"1", "id"=>"11"}}}, "commit"=>"Update Compra", "id"=>"3"}
Compra Load (1.0ms) SELECT "compras".* FROM "compras" WHERE "compras"."id" = ? LIMIT 1 [["id", 3]]
(1.0ms) begin transaction
ItemCompra Load (1.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_compras"."compra_id" = ? AND "item_compras"."id" IN (8, 9, 10, 11) [["compra_id", 3]]
ItemCompra Load (0.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_compras"."compra_id" = ? [["compra_id", 3]]
SQL (2.0ms) DELETE FROM "item_compras" WHERE "item_compras"."id" = ? [["id", 8]]
Compra Load (1.0ms) SELECT "compras".* FROM "compras" WHERE "compras"."id" = ? LIMIT 1 [["id", 3]]
ItemCompra Load (0.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_compras"."compra_id" = ? [["compra_id", 3]]
SQL (0.0ms) DELETE FROM "item_compras" WHERE "item_compras"."id" = ? [["id", 9]]
Compra Load (0.0ms) SELECT "compras".* FROM "compras" WHERE "compras"."id" = ? LIMIT 1 [["id", 3]]
ItemCompra Load (0.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_compras"."compra_id" = ? [["compra_id", 3]]
SQL (0.0ms) DELETE FROM "item_compras" WHERE "item_compras"."id" = ? [["id", 10]]
Compra Load (0.0ms) SELECT "compras".* FROM "compras" WHERE "compras"."id" = ? LIMIT 1 [["id", 3]]
ItemCompra Load (0.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_compras"."compra_id" = ? [["compra_id", 3]]
SQL (0.0ms) DELETE FROM "item_compras" WHERE "item_compras"."id" = ? [["id", 11]]
Compra Load (0.0ms) SELECT "compras".* FROM "compras" WHERE "compras"."id" = ? LIMIT 1 [["id", 3]]
ItemCompra Load (0.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_compras"."compra_id" = ? [["compra_id", 3]]
(6.0ms) commit transaction
Redirected to http://localhost:3000/compras/3
Completed 302 Found in 164ms (ActiveRecord: 12.0ms)
Log luego de dar clic en botón edit y luego en update compra
Started PATCH "/compras/3" for ::1 at 2017-07-17 06:18:50 -0400
Processing by ComprasController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"d2EEkD8dStA+Je+c1NjOXWSfDBbOgRilT
sOp037ibQNazQxFRzxc2biMrkdP1xzyDpFMarXlLGw/JEk4tP63WA==", "compra"=>{"total_bidones"
=>"125"}, "commit"=>"Update Compra", "id"=>"3"}
Compra Load (1.0ms) SELECT "compras".* FROM "compras" WHERE "compras"."id" = ? L
IMIT 1 [["id", 3]]
(0.0ms) begin transaction
ItemCompra Load (1.0ms) SELECT "item_compras".* FROM "item_compras" WHERE "item_c
ompras"."compra_id" = ? [["compra_id", 3]]
SQL (1.0ms) UPDATE "compras" SET "total_bidones" = ?, "updated_at" = ? WHERE "com
pras"."id" = ? [["total_bidones", 0], ["updated_at", "2017-07-17 10:18:50.246957"],
["id", 3]]
(5.0ms) commit transaction
Redirected to http://localhost:3000/compras/3
Completed 302 Found in 28ms (ActiveRecord: 8.0ms)
Started GET "/compras/3" for ::1 at 2017-07-17 06:18:50 -0400
Processing by ComprasController#show as HTML
Parameters: {"id"=>"3"}
Compra Load (1.0ms) SELECT "compras".* FROM "compras" WHERE "compras"."id" = ? L
IMIT 1 [["id", 3]]
Rendered compras/show.html.erb within layouts/application (1.0ms)
Completed 200 OK in 310ms (Views: 306.3ms | ActiveRecord: 1.0ms)
EDICIÓN N° 3: Agrego el log de la consola:
ItemCompra.last.destroy
ItemCompra Load (0.0ms) SELECT "item_compras".* FROM "item_compras" ORDER BY "item_compras"."id" DESC LIMIT 1
(0.0ms) begin transaction
SQL (0.0ms) DELETE FROM "item_compras" WHERE "item_compras"."id" = ? [["id", 17]]
(15.6ms) commit transaction
=> #<ItemCompra id: 17, cantidad_bidon: 8, compra_id: 5, created_at: "2017-07-21 06:09:30", updated_at: "2017-07-21 06:09:30">
La compra no se actualiza:
Compra.last
Compra Load (0.0ms) SELECT "compras".* FROM "compras" ORDER BY "compras"."id" DESC LIMIT 1
=> #<Compra id: 5, total_bidones: 8, created_at: "2017-07-21 05:56:06", updated_at: "2017-07-21 06:09:30">