It might be interesting to use Merb's run_later facilities to asynchronously update the counter cache on a periodic basis (or only if something was added to a queue).
However, it feels like the BEST option would be to add a standard database trigger and let the database handle it without the potential for problems. Perhaps this is a case for supporting arbitrary options and letting the adapters generate the appropriate SQL. So: has n, :posts, :counter_cache => true and automigrate would generate the appropriate SQL for the trigger for each supported DB. What do you think? -- Yehuda 2009/4/5 Jacques Crocker <[email protected]> > > There's no reason to ever change a counter_cache after_save. You need > a combination of several hooks (after_create, after_delete, etc). > Having it abstracted seems greatly preferred. Even with these hooks, > the count would often go out of sync so I had an update_cache method > that would synchronize everything, running once in a while. And all > this logic duplicated for each has n relationship. > > Even with this plugin, I still run into the issue that the delete > method on an association isnt implemented in Datamapper 0.9.11. So > running "user.posts.delete" returns the message "NotImplementedError: > destroy *with* validations has not be written yet, try destroy!". So > its likely I'll still need these synchronization methods that keep the > counters in sync after certain operations. > > > On Apr 5, 3:58 am, Yehuda Katz <[email protected]> wrote: > > Why is a counter cache harder than a simple after_save hook. Having never > > actually attempted to roll my own, I'm curious about what specifically is > > difficult. > > > > -- Yehuda > > > > 2009/4/5 Jacques Crocker <[email protected]> > > > > > > > > > > > > > If you're in need of counter caches in Datamapper, I found a pretty > > > nice plugin that is working great for me so far. > > >http://github.com/saimonmoore/dm-counter-cache > > > > > Maybe I'm crazy, but can't imagine anyone writing a moderate sized web > > > app and not needing counter caches. Any summary list page is going to > > > cause an N+1 craziness of count(*) queries. Rolling your own just a > > > complete mess (thats what I did before I found this plugin). > > > > > We need to get this into dm-more. The plugin is well spec'ed. Maybe we > > > should tweak the interface, I'm not sure. Dan, can you take a look and > > > see if its to your liking? > > > > > This is a followup to the thread: > > > > >http://groups.google.com/group/datamapper/browse_thread/thread/95d21c. > .. > > > > > Thanks > > > > -- > > Yehuda Katz > > Developer | Engine Yard > > (ph) 718.877.1325 > > > -- Yehuda Katz Developer | Engine Yard (ph) 718.877.1325 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/datamapper?hl=en -~----------~----~----~----~------~----~------~--~---
