Yesterday I’ve encountered some strange behavior in Rails 2.1. When I called build on an association that was empty (i.e. customer.orders.build) and then passed that association as a collection to a partial, nothing got displayed. When I called length or each {} on that association right after the call to build, however, it worked. So I sort of needed to “commit” the build call by calling length right after. I posted it to the Ruby On Rails Core group and it turned out that it was indeed a bug. It had been fixed in edge already, though. So if you’re not using edge, but the actual Rails 2.1 release, make sure to call length (i.e. customer.orders.length) after you call build on an empty association until the fixed version gets released.
Archived Post
This is an archived post from my previous blog (2007-2014). It may contain outdated information, broken links, or deprecated technical content. For current writing, please see the main Writing section.
Found a Bug in Rails 2.1... kinda
Originally published on August 20, 2008