www.flickr.com
|
Monday, May 21, 2007
Rails realities part 26 (I'm in ur app breakin cascading saves)
OK I only posted once so far from my experience at RailsConf. I may post some more about some of the sessions I attended but overall it was a great time and I learned a few new things (namely some cool functionality that I didn't know firebug had :-)
One thing I was sporadically working on was adding a new feature to PitchWire and as I went further down the rabbit hole it became apparent that to add this new feature that it's probably best that I finally bite the bullet and upgrade from rails 1.1.6 to rails 1.2.3.
I'm actually still in the process of upgrading but I thought I'd blog about a couple of things that have tripped me up thusfar.
The first one that tripped me up was after updating rails and trying to run my tests I received tons of weird command line errors that I don't have anymore unfortunately but basically my rubygems was too old (.8.something) and I needed to upgrade to the latest (.9.something). I actually can't take credit for figuring it out. My new friend Jimmy (he doesn't have a blog yet but he should) thought of it as we were sitting there pair programming as he'd already gone through the upgrade exercise.
So after updating rubygems with:
"gem update system"
I reran my tests and was down to a total of 7 failures from 120something prior. I went to the first failure and it was an application failure. For some reason my profile update action test wasn't passing.
As it turns out the problem turns out to be my reliance on behavior that was deemed to be a bug: http://dev.rubyonrails.org/changeset/4690
I was using cascading save operations to update multiple objects on a single action because on my profile page I have properties that belong to 3 different objects, user, contact_info, and company. Now one could argue that I could either have separate pages for each or only provide an AJAX style update to the properties of these objects where you can only update one at a time. I may do that in the future but for now I'm not going to refactor my application for the sake of upgrading rails.
What does all this mean? Basically when designing your rails apps don't rely on cascading save operations anywhere. Explicitly save each object whose properties you have updated in the process of a request. And since it's not really desirable to update and save multiple objects per request the best approach is to keep the data in your request confined to a single object if you can which means using fine grained AJAX style field updates using inline editors everywhere or only showing a form that edits fields for a single object.
Unfortunately this doesn't always map very well to the business logic of your app so you may have to do something like:
Where as if you can rely on saves being cascaded down through a relationship hierarchy you can just write:
I can't say I'm psyched to either patch rails back to the "broken" way or revamp my app to undo anywhere I'm relying on cascading saves but I'm a little scared at what else I might stumble across. Hopefully my tests are comprehensive enough to catch anything else, I'm glad they caught this one.
Be careful out there.
One thing I was sporadically working on was adding a new feature to PitchWire and as I went further down the rabbit hole it became apparent that to add this new feature that it's probably best that I finally bite the bullet and upgrade from rails 1.1.6 to rails 1.2.3.
I'm actually still in the process of upgrading but I thought I'd blog about a couple of things that have tripped me up thusfar.
The first one that tripped me up was after updating rails and trying to run my tests I received tons of weird command line errors that I don't have anymore unfortunately but basically my rubygems was too old (.8.something) and I needed to upgrade to the latest (.9.something). I actually can't take credit for figuring it out. My new friend Jimmy (he doesn't have a blog yet but he should) thought of it as we were sitting there pair programming as he'd already gone through the upgrade exercise.
So after updating rubygems with:
"gem update system"
I reran my tests and was down to a total of 7 failures from 120something prior. I went to the first failure and it was an application failure. For some reason my profile update action test wasn't passing.
As it turns out the problem turns out to be my reliance on behavior that was deemed to be a bug: http://dev.rubyonrails.org/changeset/4690
I was using cascading save operations to update multiple objects on a single action because on my profile page I have properties that belong to 3 different objects, user, contact_info, and company. Now one could argue that I could either have separate pages for each or only provide an AJAX style update to the properties of these objects where you can only update one at a time. I may do that in the future but for now I'm not going to refactor my application for the sake of upgrading rails.
What does all this mean? Basically when designing your rails apps don't rely on cascading save operations anywhere. Explicitly save each object whose properties you have updated in the process of a request. And since it's not really desirable to update and save multiple objects per request the best approach is to keep the data in your request confined to a single object if you can which means using fine grained AJAX style field updates using inline editors everywhere or only showing a form that edits fields for a single object.
Unfortunately this doesn't always map very well to the business logic of your app so you may have to do something like:
User.transaction do
@user.save
@user.contact_info.save
@user.contact_info.company.save
end
Where as if you can rely on saves being cascaded down through a relationship hierarchy you can just write:
@user.save
I can't say I'm psyched to either patch rails back to the "broken" way or revamp my app to undo anywhere I'm relying on cascading saves but I'm a little scared at what else I might stumble across. Hopefully my tests are comprehensive enough to catch anything else, I'm glad they caught this one.
Be careful out there.
Post a Comment