Association callbacks are not wrapped in the same transaction.

1 view
Skip to first unread message

trung

unread,
Nov 26, 2009, 7:31:00 PM11/26/09
to DataMapper
class Post
has n, comments
property :comments_count, Integer
end

class Comment
belongs_to :post
after :save do
post.comments_count +=1
post.save
end
end

#ignore the other attributes

So when I do something like

post.comments.create()

I expect the INSERT and UPDATE statements to be in the same
transaction.
Instead I see this:

INSERT into comments
BEGIN
UPDATE posts
COMMIT

I believe that ActiveRecord would wrap all the callback in the same
transaction.

So in order to guarantee transaction I have to rewrite it to this.

class Post
has n, comments
property :comments_count, Integer

def add_comments
comments_count += 1
comments.new(...)
save
end
end

class Comment
belongs_to :post
end

then call post.add_comment(...)


Thanks for reading.

trung

unread,
Nov 26, 2009, 7:32:20 PM11/26/09
to DataMapper
Reply all
Reply to author
Forward
0 new messages