Não foi possível carregar o Diqus. Se você é o moderador, por favor veja o nosso guia de problemas.
Muito massa e muito bem explicado como sempre, valeu Nando.
Muito bom! Poderia dar um exemplo simples de como utilizar essa técnica para counters dependentes de condição? por exemplo dependendo do status de "thinks",
Valeu!
Antonio, atualizei o post com um exemplo.
Show de bola Nando, no entanto, existe um pequeno problema (nao no seu código) mas como o rails trabalha com o counter caso nao seja o padrao.
Vou colocar aqui o texto com o código que mandei para os desenvolvedores do core do Rails:
Problems with :counter_cache
I found this 'problem' (I think) when was having two self relation on the table with the same table, for instance:
Having a model like that:
create_table :codes do |t|
t.string :code_key, :null => false, :limit => 25
t.string :file_name, :null => false, :limit => 200, :default => ""
t.integer :language_id, :null => false, :default => 10
t.text :code, :null => false
t.string :comment, :limit => 250, :default => ""
t.boolean :private, :null => false, :default => false
t.integer :reply_id
t.integer :reply_id_count, :integer, :default => 0, :null => false
t.timestamps
end
In the class:
# this is a self rellation....
belongs_to :reply, :class_name => "Code", :foreign_key => "reply_id", :counter_cache => true
I'm willing to have with the self rellation the counter for that.
But a problem occours when I'm try to make an insert:
SQLite3::SQLException: no such column: codes_count: UPDATE "codes" SET "codes_count" = COALESCE("codes_count", 0) + 1 WHERE ("id" = 1)
I was surprised that the rails is trying to update a column called: 'codes_count', but I don't have a column with this name!
It is using the :class_name as a prefix of the collumn _count.
Muito bom, parabéns pelo post. Muito bem explicado.
Muito bom... Tirou uma pulga atrás da minha orelha!
Falou tudo que eu queria saber e ninguem contava..
nossa... Valeu pela dica do cache no contador, já estava implementando algo na mão. Muito útil mesmo.
Assim fica difícil acrescentar algo ao texto, ele está completo! ;)
Obrigado pelo artigo!
Abraços