Has there ever been discussion of adding a method to recalculate counter cache values? This seems like it would be useful both for adding counter caches to existing tables, and also for fixing things up when direct SQL manipulations are used (performance reasons in my case). There are actually two parts to the problem. The simple part is that the update_counters only supports incremental change, so doing a full reset would require some awkward arithmetic. The more complex part is using the association reflections to calculate the actual value automatically. Doing so would require the participation of both the belongs_to reflection *and* the has_many reflection, which I can see being a bit smelly given the fact that the counter cache currently works without the need for has_many to be declared at all. However I think it''s utility might outweigh the complexity... What do you guys think? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
+1 for a public API I''ve been using this :( http://gist.github.com/247472 On Wed, Dec 2, 2009 at 20:23, dasil003 <gabriel.d@gmail.com> wrote:> Has there ever been discussion of adding a method to recalculate > counter cache values? > > This seems like it would be useful both for adding counter caches to > existing tables, and also for fixing things up when direct SQL > manipulations are used (performance reasons in my case). > > There are actually two parts to the problem. The simple part is that > the update_counters only supports incremental change, so doing a full > reset would require some awkward arithmetic. > > The more complex part is using the association reflections to > calculate the actual value automatically. Doing so would require the > participation of both the belongs_to reflection *and* the has_many > reflection, which I can see being a bit smelly given the fact that the > counter cache currently works without the need for has_many to be > declared at all. However I think it''s utility might outweigh the > complexity... > > What do you guys think? > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com<rubyonrails-core%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
https://rails.lighthouseapp.com/projects/8994/tickets/1211 Mike On Dec 2, 2009, at 1:23 PM, dasil003 wrote:> Has there ever been discussion of adding a method to recalculate > counter cache values? >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Brilliant find Mike, good work hardbap, and thanks for applying Jeremy, but I don''t actually like this API because it doesn''t let you update a single row. If you have lots of rows this will be unacceptably slow, as in slower than the amount of time it would take to manually write and execute an SQL query to do the reset. I''m going to reopen with an additional patch with how I think it should be done. On Dec 2, 11:33 am, Mike Gunderloy <larkw...@gmail.com> wrote:> https://rails.lighthouseapp.com/projects/8994/tickets/1211 > > Mike > > On Dec 2, 2009, at 1:23 PM, dasil003 wrote: > > > > > Has there ever been discussion of adding a method to recalculate > > counter cache values?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
On Wed, Dec 2, 2009 at 2:42 PM, Gabe da Silveira <gabriel.d@gmail.com> wrote:> Brilliant find Mike, good work hardbap, and thanks for applying > Jeremy, but I don''t actually like this API because it doesn''t let you > update a single row. If you have lots of rows this will be > unacceptably slow, as in slower than the amount of time it would take > to manually write and execute an SQL query to do the reset. > > I''m going to reopen with an additional patch with how I think it > should be done.Please do! Thanks, jeremy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
It''s up. Doesn''t look like I can change the ticket status though. https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1211-reset_counter_cache#ticket-1211-16 pardon the whitespace noise in the ticket also FYI, I ran tests against mysql,sqlite and postgres and had 1 mysql failure and 2 postgres failures independent of this. On Dec 2, 3:20 pm, Jeremy Kemper <jer...@bitsweat.net> wrote:> On Wed, Dec 2, 2009 at 2:42 PM, Gabe da Silveira <gabrie...@gmail.com> wrote: > > > Brilliant find Mike, good work hardbap, and thanks for applying > > Jeremy, but I don''t actually like this API because it doesn''t let you > > update a single row. If you have lots of rows this will be > > unacceptably slow, as in slower than the amount of time it would take > > to manually write and execute an SQL query to do the reset. > > > I''m going to reopen with an additional patch with how I think it > > should be done. > > Please do! > > Thanks, > jeremy-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.