# Counter\_cache is making a redundant SELECT before UPDATE

**URL:** <https://www.ruby-forum.com/t/counter-cache-is-making-a-redundant-select-before-update/169246>\
**Category:** Rails\
**Created:** [June 1, 2009, 11:42am UTC](https://www.ruby-forum.com/t/counter-cache-is-making-a-redundant-select-before-update/169246 "2009-06-01T11:42:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [June 1, 2009, 11:42am UTC](https://www.ruby-forum.com/t/counter-cache-is-making-a-redundant-select-before-update/169246/1 "2009-06-01T11:42:12Z")

</div>

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) ?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [June 1, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/counter-cache-is-making-a-redundant-select-before-update/169246/2 "2009-06-01T12:05:56Z")

</div>

On Jun 1, 10:48 am, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [June 1, 2009, 11:48am UTC](https://www.ruby-forum.com/t/counter-cache-is-making-a-redundant-select-before-update/169246/3 "2009-06-01T11:48:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [June 1, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/counter-cache-is-making-a-redundant-select-before-update/169246/4 "2009-06-01T12:35:39Z")

</div>

> 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

Thank you Frederick for your message 🙂

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [June 1, 2009, 9:43pm UTC](https://www.ruby-forum.com/t/counter-cache-is-making-a-redundant-select-before-update/169246/5 "2009-06-01T21:43:16Z")

</div>

Quoting F. Perez [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
