Rails 2.1 gotcha with has_one

I just updated the SaaS Rails Kit for Rails 2.1, and added a couple new payment gateways, and I thought I'd mention a behavior change in Rails 2.1 that might catch you unawares.

The new behavior in Rails 2.1 for has_one is that Rails will now automatically validate the association for you, which can cause you some problems if you already were validating the association yourself, as I do with the SaaS Rails Kit. In my case, I validate the subscription associated with a new account, but only if everything is valid with the account first, as I don't need to hit the gateway with your credit card info if your account info is invalid. When I started to upgrade the Kit to Rails 2.1, my spec that checks that behavior started failing, as it got back the generic "is invalid" error message, rather than the error message I was generating inside the Subscription model.

So, I thought I'd put this blog post out there to hopefully save some people some time wondering what's going on, and also to give you a good example of why thoroughly testing your code is a good idea. :)

Comments