episode 4 - counter_cache gotcha
Trevor Turk
4 posts
|
Thanks again for your screencasts here and on Railscasts. I noticed that you used the “execute” command and custom SQL when establishing the correct count when adding a counter_cache after you had some data in the database already. I just wanted to point out that this is a potential “gotcha” because I have tried to do the same with the update_attribute command with no luck. I’m not sure why, but it seems that you need to use custom SQL in this case, and it drove me crazy for a couple of hours before I decided to go straight into the custom SQL route. I thought that might be worth pointing out, since I’m sure others will run into the same issue. Anyway, thanks again! |
Ryan Bates
25 posts
|
Yeah, if you use update_attribute or something similar it’s important to reset the column information after adding the column. This is because Rails caches the columns and it doesn’t know if you added one.
However, with Rails 2.1 I’ve noticed this sometimes doesn’t work. I think it has to do with dirty tracking, but I was never able to reliably duplicate the problem so I don’t know what it was exactly. |
2 posts, 2 voices
