when you do a #new, nothing is done in the DB (like a transaction, i
suppose). So what you can do is something like:

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.

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?


On Apr 26, 8:33 am, Simon Arnaud <[email protected]> wrote:
> Following the "chicken and egg problem with Models", I was wondering
> if there was a way to create associated models easily without having
> to use temporaries class.
>
> Lets say a simple thing as blog post and comments.
>
> class Post < Sequel::Model
>   many_to_one :author
>   one_to_many :comments
> end
>
> class Author < Sequel::Model
>   one_to_many :posts
> end
>
> class Comment < Sequel::Model
>   many_to_one :post
> end
>
> Considering authors does not need to be created, I want to create a
> post, with a few prefilled comments.
>
> I'm wondering if there are ways to code this as to construct the post
> and the comments, and depending on user choice, commit it, or just
> throw everything. Something like :
>
> post = Post.new(:title => "foo", :author => Author.first(:name => "whatever")
> post.add_comment(:text => "...")
> ask_user_if_ok do |response|
>   if reponse == 'ok' then
>     post.create
>   else
>     post.destroy
>   end
> end
>
> I think transaction can solve this problem, but I'm not sure how I
> would call it from sequel without the block syntax.
>
> Moreover, while this seems here very straightforward, the post might
> be created (not in the database) well ahead of the comments. The
> comments are attached through gui callbacks. And the user response is
> from either a cancel or save button.
>
> 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 
> athttp://groups.google.com/group/sequel-talk?hl=en.

-- 
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