I picked up a neat TextMate trick while pair programming with Justin Gehtland on Monday. Check out a directory from a Subversion repository and open it in TextMate. Click on the root directory of the project and hold down Ctrl-Shift-A. A menu will appear allowing you to select from a number of SVN options, including adding and deleting from the repo, diffing files, and so on. Very nice, especially if you’d prefer not to jump to the command line for such actions.
Author: Matthew
-
Creating service apps with Rails at tonight’s Ruby meetup
This is another brief reminder that I’ll be giving a preview of my RailsConf talk at tonight’s Ruby meetup. The meetup starts at 7 PM at Red Hat headquarters. Hope to see you there!
-
Preview my RailsConf talk at this month’s Ruby meetup
For those who live in the RTP area, I’ll be giving a preview of my RailsConf talk at the Raleigh-area Ruby Brigade meetup on April 17th. We meet at Red Hat headquarters on NCSU’s Centennial Campus at 7:00 PM. I’ll be talking about Teascript, homesteading, and building niche web apps that generate passive income. The talk is pretty solid, but I’ll still be asking for feedback from the group on what can be added or improved. See you there!
-
Turn finders into associations and get caching for free
Let’s say we have a method on a model that looks something like this:
def last_assignment assignments.find(:first, : order => 'created_at DESC') end
We call this method several dozen times from various other methods on the model. The problem is, every time we call the method a new database query is triggered. This happens even if we make multiple calls within the same method. For example:
def last_assignment_is_old? last_assignment && last_assignment.completed_at && last_assignment.completed_at < 30.days.ago end
This results in three identical queries to the database. How wasteful! Let's fix it:
def last_assignment @last_assignment ||= assignments.find(:first, : order => "created_at DESC") end
Ahhhh, this is much better. Now we're storing the result as an instance variable on the model. Our
last_assignment_is_old?method will only trigger a single call to the database now since we're caching the result. But what happens if we do something like this?puts model.last_assignment.nil? model.assignments.clear puts model.last_assignment.nil?
On the console, we should see
falseand thentrue, right? What we actually see isfalseandfalse. Even though we clear out the assignments from the model, the last assignment is still stored in the@last_assignmentinstance variable. Since we're dealing with the same instance of the model, the cached value is returned. In this particular case, that's not good!The way to get around this is by using an association:
has_one :last_assignment, :class_name => "Assignment", : order => "created_at DESC"
This association basically says, "Sort the assignments by the creation date and give me the first one in the list." After running our test code again, we're golden. It turns out that Rails' associations provide caching automatically. All we have to do is remember to call
reloadon our model before querying it again. This will ensure that we're working with fresh data:puts model.last_assignment.nil? model.assignments.clear puts model.reload.last_assignment.nil?
Now we get
falseand thentrueas expected. Thereloadtrick won't work with instance variable caching, unfortunately, which is why the association is a better choice in this case.After examining a few more of our models, we discover that there are many such finder methods that can be converted to associations. We quickly begin converting them, hoping nobody notices that we've been senselessly operating without freebie caching for so long.
-
Multiparameter assignment validation
Have you ever realized that the default Rails date helpers allow invalid dates to be selected? I ran into an issue yesterday where one of my Teascript users attempted to select a date of November 31, 2007. That date obviously doesn’t exist. Instead of failing with a validation error, however, Rails threw a
MultiparameterAssignmentErrorsexception.This is the first time I’ve run into this particular problem in Rails, and I’ve been using the framework for over two years now. Suffice it to say the odds of running into it are slim, but how should it be handled once it occurs? I found a clean, if not particularly elegant, solution in the Validates Multiparameter Assignments plugin. Once the plugin is installed, adding a single line to your model is all that’s required:
class User < ActiveRecord::Base validates_multiparameter_assignments end
The plugin causes any multiparameter assignment exceptions to be surfaced as validation errors, which is exactly what I needed to happen. Why don't I think it's elegant? Simply because it takes the shotgun approach by assuming that I want to validate all multiparameter attributes. I'd prefer to specify which attributes I want validated. This is a minor nit, though. The plugin does work quite well.
There is one other customization that can be made to the plugin, but I've got to leave some surprises for you so check out the wiki page to familiarize yourself with the plugin. If you don't need the plugin now, I can pretty much guarantee you'll need it eventually.
Update: This plugin is unnecessary as of Rails 2.0.2 because invalid dates are now gracefully offset to the next valid day without causing a validation error.
-
Rails in the Enterprise
Tim O’Brien recently cataloged the top four myths that often prevent Rails from being used in Enterprise settings. From performance to flexibility, he does a thorough job covering many the objections that I’ve heard raised myself when proposing Rails as a possible solution. It can be frustrating to be in that situation, but Tim provides some “real answers” that you can fire back when challenged. This is a fascinating read, even for someone who isn’t plugged in to a large corporation right now.
-
RubyConf in the southeast
The Ruby community in the Research Triangle Park area of North Carolina has been growing by leaps and bounds lately, in large part due to the fantastic job Nathaniel Talbott has been doing with the local Ruby Brigade. Another sign of the growing interest in Ruby and Rails in the area is the just-announced Ruby Hoedown, a RubyConf of sorts that’s being hosted by the Brigade in the Raleigh area on August 10th and 11th. Details are sketchy, but you can register on the site to receive more information as it becomes available. And to all you aspiring speakers, now is the time to start thinking about what you’d like to present at the conference!
-
First Agile RTP meeting tonight
This is just a reminder from your friendly neighborhood agilist that the new Agile RTP user group has its first meeting in Cary tonight. Read more about it on Jared’s blog. This sounds like a great opportunity to meet other agile-minded people in the area and listen to an interesting talk. Oh, and let’s not forget the bragging rights secured by those who participate in the first meeting of a new user group.
-
Highrise is now taking leases
37 Signals’ latest web application, Highrise, is now open for business. They offer a free account so you can try out the service without laying down any dinero. Looks like a useful tool. It’ll be interesting to see how tightly it integrates with Basecamp.
-
Fantastic Foxmarks
Have you wondered how to effectively synch bookmarks between work and home? I discovered the Foxmarks plugin for Firefox almost by accident last week and it does the trick for me. Better still, it’s fast and lightweight, the perfect companion for an agile developer.Foxmarks installs into Firefox in seconds. Once running, all we need to do is hit Command-Shift-S to synch our bookmarks with a remote server. Hop on to a second system, install the plugin again, and hit Command-Shift-S to download the bookmarks we just synched. End of story? Not quite. What if we add a different bookmark on both computers at the same time? Foxmarks is smart enough to handle merging both bookmarks the next time we synch.
Foxmarks is a really wonderful tool and I highly recommend it. I no longer have to worry about my bookmarks getting out of synch between the computers that I work on. This has saved me a lot of time and angst.