I thin I solved it.

Using "Article.transaction" works.

Why is "DataMapper.repository(:default).transaction" not working?


On Jun 10, 8:11 pm, Javier <[email protected]> wrote:
> Hi, I'm already using this:
>
> DataMapper::Model.raise_on_save_failure = true
>
> But I'm not getting any exception from the transaction
> Code just evaluates correctly, but always false (@article.saved?) even
> passing correct values to constructors.
>
> For example, this code doesn't work as always evaluates as False on
> the "if":
>
>         @article = Article.new(params[:article])
>         @stock_line = StockLine.new(
>                 :work_center => default_work_center,
>                 :units => @qty,
>                 :available => @qty
>         )
>
>         DataMapper.repository(:default).transaction do
>                 @article.save
>                 @stock_line.article = @article
>                 @stock_line.save
>         end
>
>         if @article.saved?
>         ......
>
> now the same code, but with transaction removed, works correctly:
>
>         @article = Article.new(params[:article])
>         @stock_line = StockLine.new(
>                 :work_center => default_work_center,
>                 :units => @qty,
>                 :available => @qty
>         )
>
>         #DataMapper.repository(:default).transaction do
>                 @article.save
>                 @stock_line.article = @article
>                 @stock_line.save
>         #end
>
>         if @article.saved?
>         ....
>
> I'm passing exactly the same values in every try.
> I'm missing something...
>
> On Jun 10, 6:39 pm, Emmanuel Gomez <[email protected]> wrote:
>
>
>
>
>
>
>
> > 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 `[email protected]` 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 
> > > athttp://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