#343 ✓resolved
ronin-22245 (at lighthouseapp)

problem with before :save and newly created objects

Reported by ronin-22245 (at lighthouseapp) | June 11th, 2008 @ 11:45 AM

After upgrading to 0.9.1, I've run into some problems with saving objects. It appears that if you define a 'before :save' callback and create a new object, DM somehow can't handle the callback properly and throws an exception.

class Article
  include DataMapper::Resource

  property :title, String

  before :save do
    # do something

@article = Article.new({:title => 'A sample article'})
@article.save # -> throws an exception

Exception details:

super: no superclass method `save' - (NoMethodError)

/usr/lib/ruby/gems/1.8/gems/dm-validations-0.9.1/lib/dm-validations.rb:35:in `save'

/usr/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/data_mapper/hook.rb:112:in `call'

/usr/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/data_mapper/hook.rb:112:in `save'

/usr/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/data_mapper/hook.rb:108:in `catch'

/usr/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/data_mapper/hook.rb:108:in `save'

When I changed before :save to before :create everything worked fine.

Comments and changes to this ticket

  • Brent Dillingham

    Brent Dillingham June 11th, 2008 @ 11:45 AM

    I'm having this same issue. See my pastie:


    I encountered it first by requiring both dm-validations and dm-timestamps. I eliminated dm-timestamps from the equation by creating a before :save filter that does the same thing it does.

    A few good fellows in the #datamapper had me shuffle around the order in which dependencies are in Merb's config/init.rb, but to no avail. The most suggested order was this:

    use_orm :datamapper

    require 'dm-validations'

    Those are the only two dependencies in my application at the moment.

  • Jox

    Jox June 10th, 2008 @ 03:25 AM

    You can temporarily circumvent this problem by defining

    before :create
    before :update

    instead of

    before :save

    I had to do this with is-nested_set.

  • Carl Lerche

    Carl Lerche June 11th, 2008 @ 05:57 PM

    Is this related to the bug I just reporte? #371

  • Michael Leung

    Michael Leung June 11th, 2008 @ 07:13 PM

    I am having this issue as well. We just updated our OSS blog (featherblog.org) to DM 0.9.1 and when calling save on some models we also get: super: no superclass method `save' - (NoMethodError).

    The thing is, I can't really see a clear pattern to it. I thought it might be what some of the other users reported in this ticket about the callbacks. So I changed them to before :create, :before and still received the error. Then I changed the order of:

    include DataMapper::Resource

    include DataMapper::Validate


    include DataMapper::Validate

    include DataMapper::Resource

    And that worked. However in a model with no callbacks at all, and just an include to Resource we still get the error.

  • Michael Leung

    Michael Leung June 11th, 2008 @ 07:15 PM

    Sorry I didn't mean before :create, :before, I meant before :create, and after :create.

  • Carl Lerche

    Carl Lerche June 12th, 2008 @ 05:49 PM

    I really believe that this is related to #371, which I fixed. So, if you can grab the latest master (dm-core AND dm-more), please test this again. If it still fails, please let me know.

  • Brent Dillingham

    Brent Dillingham June 14th, 2008 @ 01:51 PM

    Sweet! Just confirmed that your fix does appear to have worked, Carl. Nicely done, and thanks.

  • ronin-22245 (at lighthouseapp)

    ronin-22245 (at lighthouseapp) June 26th, 2008 @ 09:06 AM

    • Tag set to dm-more

    Again, before and after hooks don't seem to work at all. Even the "before :update/:create fix" doesn't help...

  • Bernerd Schaefer

    Bernerd Schaefer August 1st, 2008 @ 09:54 AM

    • State changed from “new” to “resolved”

    There has been major work done on fixing our hooks, so this should no longer be a problem.

  • cheba

    cheba August 21st, 2008 @ 10:59 AM

    In what revision it was fixed? It seems that in edge DM hooks does not work. At least I had no success with before :save and before :create.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket


Referenced by