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.

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

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 :slight_smile:

Quoting Fernando Perez <rails-mailing-list@andreas-s.net>:

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