2010/4/26 mooman <[email protected]>:
> when you do a #new, nothing is done in the DB (like a transaction, i
> suppose). So what you can do is something like:

I know :), that's why I use new, and not create.

>
> post = Post.new(:title => "foo", :author => Author.first(:name =>
> "whatever")
> post.add_comment(:text => "...")
> # then however you get the response
> if response == 'ok' then
>  post.save
> end
>
> so you dont really have to "destroy" anything since it wouldn't have
> been created in the first place unless user says ok.

The point is that this code does not work, as it won't allow you to
add comment to an unsaved post. It would have been wonderfull if it
did :).
I agree destoy is useless.

>
> if you'd like to do something with transaction (only if you're already
> modifying stuff in the DB is when this is useful), you can rollback
> the transaction like this:
>
> Post.transaction do
>  # do stuff
>  if some_condition then
>    raise Sequel::Rollback
>  end
> end
>
> which will rollback the transaction, but wont actually raise an
> error.
>
> will that work for you?

Not at all. My point was that I cannot do all this in a block
(Post.transaction do), as my post will be instancied through a
callback, then my comments will be added through other(s) callback(s),
and finally, it will be saved through another callback.

So I need, in pseudo ruby

def on_create_window
  begin_transaction
  post.create(...)
end
def on_add_comment(post)
  post.add_comment(Comment.create(...))
end
def on_user_save(post)
  post.save
  end_transaction
end
def on_user_cancel()
  rollback_transaction
end

regards

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to