Nov
29
I am sure if you read my blog, you are aware that Steelpixel Hosting has been “acquired” by RailsPlayground for an undisclosed sum of nothing (read: $0). I made a major MAJOR mistake with the hosting server and the easiest route was to just move people to a new service. The control panel hard drive died, it was also our “backup” server and we didn’t have any external copies of the customer data. Luckily the websites were intact on another server, we just lost CC and control panel data.
The current blog you are reading is on my new private server (vps) running both nginx (rails) and litespeed (php). I have almost all my sites moved over to this box, I think I only have 4 or 5 more to move at this point. I am also thinking about setting up a new blog geared more towards my rails work, I find a lot of nifty tips that I feel should be shared because the search results on these issues are totally lacking. I will link to it once I get it up.
Tasty Planner our 1st place app, has been updated. We have really revamped the Menu Planning, Grocery List Creator, our Chef list, and the Recipe Sharing portions. We added popularity algorithms to recipes, changed our backend search engine (sphinx now), focused on SEO a bit, and tried to really clean up the UI. You can also find a brand spankin new Recent Recipe Atom feed if you are curious to follow new recipes up to the minute :).
Nov
3
Rails Edge Change: How to add a counter cache to an existing db table
Filed Under General | 6 Comments
A counter cache is a drop dead simple feature that makes counting child objects much faster. It allows you to keep a database field on the parent object, with the integer in it. Rails will update the counter for you when an association is made between the parent and child objects. This allows you to do a simple SQL query for the counter stored on the parent object, rather than counting *all* the associated objects each time. Very handy, but something changed in edge (2.0) recently that made all the instructions on tutorial sites stop working.
Counter cache has become a attr_readonly attribute, and as such you can’t directly mess with it and have the changes saved when you call object.save. Take this railscast example and give it a shot against the latest edge build… You will end up with all 0’s in the count column. So what is a guy/gal supposed to do to get some Counter Cache action into a db table for objects that are already in production? Let me show you my edge based migration…
def self.up
add_column :recipes, :saved_recipes_count, :integer, :default => 0
Recipe.reset_column_information
Recipe.find(:all).each do |r|
Recipe.update_counters r.id, :saved_recipes_count => r.saved_recipes.count
end
end
So if you note, we are using update_counters instead of update_attributes. The update_counters call takes two arguments, the first arg is the id of the object and the second arg is a hash of counters with the amount to increment by (or decrement if the number is negative). The update_counters call is the same thing rails uses behind the scene to update your counters when you add/remove child objects.
Hopefully someone will find this stuff helpful, it drove me nuts for about a half hour the other night.