During the Hackfest in January I put together a patch for an enhancement to Rails dynamic finders. It was just committed today (thanks, Jeremy!), so now's a good time to do a little explanation of it. It's just a little thing, so this should be brief.
Here's a typical situation:
tag = Tag.find_by_name(name) if tag.nil? tag = Tag.create(:name => name, :creator => current_user) end
You'd think the way to do that would be with the
find_or_create_by_name dynamic finder, but that doesn't work since there's no way to search by name only, but create by name and creator. Well, that's just not right. So here's what we'll do:
Tag.find_or_create_by_name(:name => name, :creator => current_user)
The new feature is that if you pass a hash to the finder, it will still use only the values named in the finder to find the object, but it will create a new object using all the values.
It's not a big deal or something I use every day, but I like it anyway.