Usando Rails 3.1.3 y estoy tratando de averiguar por qué nuestros cachés de contador no se están actualizando correctamente al cambiar el id. De registro padre a través de atributos_actualización.Rails counter_cache no se actualiza correctamente
class ExhibitorRegistration < ActiveRecord::Base
belongs_to :event, :counter_cache => true
end
class Event < ActiveRecord::Base
has_many :exhibitor_registrations, :dependent => :destroy
end
describe ExhibitorRegistration do
it 'correctly maintains the counter cache on events' do
event = Factory(:event)
other_event = Factory(:event)
registration = Factory(:exhibitor_registration, :event => event)
event.reload
event.exhibitor_registrations_count.should == 1
registration.update_attributes(:event_id => other_event.id)
event.reload
event.exhibitor_registrations_count.should == 0
other_event.reload
other_event.exhibitor_registrations_count.should == 1
end
end
Esta especificación falla, lo que indica que el contador de la memoria caché en el evento no se está reduciendo.
1) ExhibitorRegistration correctly maintains the counter cache on events
Failure/Error: event.exhibitor_registrations_count.should == 0
expected: 0
got: 1 (using ==)
¿Debo esperar incluso que esto funcione o debo hacer un seguimiento manual de los cambios y actualizar el contador yo mismo?
Gracias @ mu-is-too-short Esto definitivamente soluciona el problema. Creo que esto ciertamente merece atención en ActiveRecord, voy a buscar enviar un parche. –
@MichaelGuterl: Genial, no olvides incluir una actualización de la documentación con tu parche :) –
@MichaelGuterl: Tal vez quieras probar el enfoque de Ben también. Repaso el código de Rails nuevamente para ver si me perdí algo. Esto podría ser solo un error y una documentación pobre/incompleta. –