#845 ✓resolved
Marcin Kulik

Updating parent object fires a hook on associated objects

Reported by Marcin Kulik | May 7th, 2009 @ 08:45 AM

I've noticed a bug in (probably) dm-observer causing firing hooks on associated objects. If you set up "after :update" observer for Comment model and then you update some Post record the hooks are being fired on all comments objects. Following spec is reproducing the problem on DM 0.9.11


require 'rubygems'
require 'spec'
require 'dm-core'
require 'dm-observer'

DataMapper.setup(:default, 'sqlite3::memory:')

class Post
  include DataMapper::Resource

  property :id, Serial
  property :name, String

  has n, :comments
end

class Comment
  include DataMapper::Resource

  property :id, Serial
  property :body, String

  belongs_to :post
end

class CommentObserver
  include DataMapper::Observer
  observe Comment

  after :update do
    raise RuntimeError
  end
end

DataMapper.auto_migrate!

describe Post do

  before(:all) do
    post = Post.create(:name => "Jola")
    post.comments.create(:body => "A comment")
  end

  it "should not fire hooks on associated objects after updating (association not loaded)" do
    post = Post.first
    lambda do
      post.update_attributes(:name => "Misio")
    end.should_not raise_error
  end

  it "should not fire hooks on associated objects after updating (association loaded)" do
    post = Post.first
    post.comments
    lambda do
      post.update_attributes(:name => "Misio")
    end.should_not raise_error
  end

end

Comments and changes to this ticket

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

Pages