Hi guys,
I have these models:
class Compra < ActiveRecord::Base belongs_to :proveedor has_many :detalle_de_compras has_many :lotes, :through => :detalle_de_compras
accepts_nested_attributes_for :detalle_de_compras, :allow_destroy => true
def nombre_proveedor self.proveedor.try(:nombre) end end
class DetalleDeCompra < ActiveRecord::Base belongs_to :compra, :counter_cache => true belongs_to :lote
accepts_nested_attributes_for :lote end
The problem is that when I delete a "DetalleDeCompra" through an update to "Compra" using nested attributes (with the _delete param set to something that evaluates to true) the counter cache is decreased twice.
I don't know if it's worth mentioning that I'm sending data for a 2 level deep nested structure. This is the deepest model I'm using:
class Lote < ActiveRecord::Base belongs_to :estado_del_lote belongs_to :producto has_one :detalle_de_compra has_one :compra, :through => :detalle_de_compra
def nombre_producto self.producto.try(:nombre) end
def nombre_estado_del_lote self.estado_del_lote.try(:nombre) end
def fecha_compra self.compra.try(:fecha) end end
Any ideas on what's wrong here?
P.S. Here is the migration to the model holding the counter cache:
class CreateCompras < ActiveRecord::Migration def self.up create_table :compras do |t| t.integer :proveedor_id t.date :fecha t.date :fecha_de_pago t.string :condicion_de_pago t.integer :detalle_de_compras_count, :default => 0
t.timestamps end end
def self.down drop_table :compras end end