Fernando Perez
2009-Jun-01 09:42 UTC
counter_cache is making a redundant SELECT before UPDATE
Hi, I have the following code: Message, belongs_to :topic, :counter_cache => true topic = Topic.find_from_permalink(params[:id]) topic.messages.create(params[:message]) When the message gets created, then AR issues a supplemental SELECT to retrieve the message''s topic and then updates its messages_count. Why is that happening? If I do it manually: if topic.messages.create(params[:message]) topic.messages_count += 1 topic.save! end Then I don''t get this additional SELECT. Does the sql cache only work when I do Topic.find(2) ? -- Posted via http://www.ruby-forum.com/.
Fernando Perez
2009-Jun-01 09:48 UTC
Re: counter_cache is making a redundant SELECT before UPDATE
Hmm, actually I just watched the railscasts about the counter_cache, and in his log he also has this redundant SELECT that he didn''t even spot. So I guess this could be an additional feature added to Rails to save 1 DB query. -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Jun-01 10:05 UTC
Re: counter_cache is making a redundant SELECT before UPDATE
On Jun 1, 10:48 am, Fernando Perez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hmm, actually I just watched the railscasts about the counter_cache, and > in his log he also has this redundant SELECT that he didn''t even spot. > > So I guess this could be an additional feature added to Rails to save 1 > DB query.This is basically rails not knowing about inverse associations (which it will do in the next version). If you do do itself you should use increment_counter rather that doing what you show above (which has a race condition). Fred> -- > Posted viahttp://www.ruby-forum.com/.
Fernando Perez
2009-Jun-01 10:35 UTC
Re: counter_cache is making a redundant SELECT before UPDATE
> This is basically rails not knowing about inverse associations (which > it will do in the next version). > If you do do itself you should use increment_counter rather that doing > what you show above (which has a race condition). > > FredThank you Frederick for your message :-) -- Posted via http://www.ruby-forum.com/.
Jeffrey L. Taylor
2009-Jun-01 19:42 UTC
Re: counter_cache is making a redundant SELECT before UPDATE
Quoting Fernando Perez <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Hmm, actually I just watched the railscasts about the counter_cache, and > in his log he also has this redundant SELECT that he didn''t even spot. > > So I guess this could be an additional feature added to Rails to save 1 > DB query.Is the title declared unique? Then Rails queries the DB to check that there is no existing record before doing the UPDATE. HTH, Jeffrey
Seemingly Similar Threads
- counter_cache, has_many and belongs_to
- what happened to my increment_counter method?
- Ferret FileNotFound error after adding counter_cache to mode
- counter_cache is not looking for children_count with acts_as_tree
- acts_as_tree counter_cache behavior is different than API docs