9

Is it possible to use counter cache on a habtm relation in rails 3 ?

I really need it to speed up my application.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102

3 Answers3

3

There is no built in method for HABTM but it is simple to make your own counter cache (actually I was doing it many times before I've invented counter_cache.

Or use has_many :throught instead of HABTM association. I never use HABTM because it looks awkward for me.

Related topic:

counter_cache with has_many :through

Community
  • 1
  • 1
fl00r
  • 82,987
  • 33
  • 217
  • 237
2

For what it's worth, I created a version for HABTM which you can see here.

If you have any questions/comments/suggestions, feel free to leave them - or even fork it and let me see :)

marcamillion
  • 32,933
  • 55
  • 189
  • 380
0

In rails 4, counter_cache for has_and_belongs_to_many relation works great.

Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
Bogdan Guban
  • 336
  • 4
  • 4