Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

transactions don't fail in 3.0

#3214

Section 20.4 of Agile Web Development with Rails describes how transactions can be used to prevent things such as negative balances from being stored in a database. Doing so fails (as expected) in Rails 2.3.4 (and all prior versions of Rails), but doesn't fail (as you would hope it would) with Rails 3.0

Full scenario: http://intertwingly.net/projects/AWDwR3/checkdepot.html#section-20

The script itself is standalone, and I'm attaching it here. To run from a checked out version of Rails, set RUBYLIB as appropriate (see comment in the script); otherwise it will attempt to use the gem version of Rails installed on your system.

Run the script, passing in a single argument of '2' to see the behavior, thus:

ruby transactions.rb 2

Reported by Sam Ruby · September 15th, 2009 @ 09:53 PM

State: resolved
Milestone: 3.0.2
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • Milestone cleared.
    • State changed from new to open
    • Assigned user set to Jeremy Kemper

    The root problem here is that the 'validate' method isn't being called, so the record saves. Looking into it.

    September 16th, 2009 @ 05:45 AM

  2. Daniel Guettler
    Daniel Guettler

    The reason why this is failing is because validate is a private method and overriding 'validate' is a deprecated callback. So this calls 'deprecated_callback_method' which checks if the object responds to the passed method. If the method is private it will return false and 'validate' is never executed. If the method is not private validation works fine.
    It also works if the new syntax Account.validate(:balance_positive) is used. There it doesn't matter if the method is private or not.

    January 18th, 2010 @ 04:55 AM

  3. Daniel Guettler
    Daniel Guettler

    Upps, this should say: 'validate is defined as private in the given example'...

    January 18th, 2010 @ 04:57 AM

  4. Repository
  5. José Valim
    José Valim
    • State changed from open to resolved

    Thanks Daniel, the information you provided helped fix the issue.

    January 18th, 2010 @ 08:59 AM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM