Really Common Rails Gotchas

The following patterns in Rails lead to relatively silent test failure. I stumble over them all the time.

  • ActiveRecord models not saving when I expect them to. The most common cause is that the creation of the object fails a validation. Often this causes a test failure down the line because a record that was supposed to be found in the database isn’t there—because it didn’t save. Using a factory tool helps with this, as does using save! and create! in test setup to have the failure happen at the point of the code problem and not further down the line.

  • In versions of Rails that use attr_accessible, Rails silently ignores inaccessible parameters in mass assignment. Rails 4 will still ignore parameters not specified ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.