Javier,

In your first example, you were dealing with basic Ruby behavior: variables 
defined inside of blocks (`DataMapper.repository(:default).transaction do .... 
end`) are block-local, and don't exist in the enclosing (outer) scope.

In your second example, there are many things that could prevent the Article 
from being saved. One way to handle this is via exceptions:

@article = Article.new(params[:article])
@article.raise_on_save_failure = true

With the `raise_on_save_failure` option set to true, any failed #save or 
#update called on @article will cause an exception to be raised. This will 
cause the transaction to be rolled back. Of course, you will have to catch the 
exception (DataMapper::SaveFailureError) in your code and handle the failure 
appropriately.

However, none of that will tell you _why_ the #save call failed. That's what 
Validations are for. Check `@article.errors` for messages about validations 
errors that may have prevented the save.

-- Emmanuel

On Jun 10, 2011, at 9:55 AM, Javier wrote:

> Hi, I'm trying to use transactions in my code but I'm experiencing
> some difficulties.
> 
> My code looks like this:
> 
> post "/article/new" do
>       @user = User.get(session[:uid])
>       @qty = params[:qty].to_i
> 
>       DataMapper.repository(:default).transaction do
>               @article = Article.create(params[:article])
>               @article.stock_line = StockLine.create(
>                       :work_center => default_work_center,
>                       :units => @qty,
>                       :available => @qty
>               )
>       end
> 
>       if @article.saved?
>               @status = "Ok"
>               @article = Article.new
>       else
>               @status = "ERROR"
>       end
> 
>       erb :"article/new"
> end
> 
> When I execute the code on my browser (this is a sinatra app), I get
> the following error:
> 
> No method "saved?" for Nil Class
> 
> So I assume that @article only exists inside the transaction object.
> So I tried creating the object in the outside with this:
> 
>     @article = Article.new(params[:article)
> 
>     DataMapper.repository(:default).transaction do
>          @article.save
>          .....
>     end
> 
> In that case, @article.saved? always returns False.
> 
> What am I doing wrong? How can I check if an entire transaction has
> failed?
> 
> Thanks
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "DataMapper" 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/datamapper?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" 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/datamapper?hl=en.

Reply via email to