It’s the first morning at OSCON, and I’m taking the tutorial Advanced ActiveRecord. It’s being given by Gregg Pollack and Jason Seifer of RailsEnvy. We’re at the mid-session break, so I’ve got a few minutes to jot down my thoughts.
So far, this is really valuable. We started with looking at the various ways you can use ActiveRecord and relationships between models in your Rails code. For each approach, we saw how that was actually translated into database queries, and how different approaches in Rails gave very different performance as a result. Obviously, this will change over time, but the idea of looking at how ActiveRecord methods actually execute will remain valuable.
We then looked at some performance tweaks, mostly consisting of adding indexes. That’s nothing special for ActiveRecord, per se, but it’s good to see how to do it in Rails and how to notice when you’ll want it.
The rest of the pre-break session was looking at new methods of dealing with polymorphic relationships. For example, if you’ve got packages that might be transported by car, truck, or bike, and what to know which car, truck, or bike is being used, you’d traditionally relate the package model to three different models, one for each transport type. Now you can handle this kind of situation with a single model by adding a field (or fields, depending on the situation) and new relationship properties.
I’m not going to give examples here. I’ve successfully been doing the exercises, but I’m not fluent enough with Rails to explain this coherently yet. Some exercises and hints relating to this are available online.
After the break:
This is nice. Rails 2.1 tracks dirty objects (changed but not yet saved). This makes updates more efficient (it’s smart enough to not have to save all the fields, but only the ones that have changed. And it not only marks it as dirty or not, it even keeps the old and new values until it has been saved.
One gotcha for that: dirtiness is triggered by assigning a value to a model field. Change a field some other way and it’s not marked.
Callbacks: don’t override the standard ones (someone else might do it, too). Add new ones of your own.
Observers: classes that trigger when other classes change.
Unfortunately, I can’t do any of these after-break exercises because I’d have to remember too much detailed syntax that’s new to me. That’s shown in the slides, but by the time I start the exercises the slides are gone. The pre-break exercises all had some sample code on-line that helped with this. It would be an enormous help to have copies of the slides they’re using so I could refer back to them.
Sharding: breaking one logical database into multiple physical ones, with different subsets of related records in each physical database. Five Runs has released a gem to do this for you. This generated a lot of discussion this morning about whether this is appropriate for Rails to worry about instead of leaving it all to the database system. Adam Keys of Five Runs makes a good case for putting it in the application instead (and I’ve run into many cases where putting “database” stuff in my application code instead seems to make things much clearer, more maintainable, and faster, so this rings true to me).
The discussion led to the general concept of how to create a plug-in for Rails. I think we’re about to have an exercise on this that there’s no chance I’ll be able to do during the talk. Oh well, I can do them later when I have time to find references on the pieces I need.
All in all, this has been a very worthwhile talk, but it could have been better if:
- The slides were available, preferably electronically
- Solutions to the exercises were available for download along with the exercises
- There was at least some reference material for the last three exercises like there is online for the first three ones.
I know I’ll be using a lot of this material at work soon.