mardi 25 avril 2017

Rails save a object inside a method

I have a method Order that if the status is completed decrease the product quantity on the method save and create a transaction record.

Everything works fine except that in the logs shows:

even showing the code still works

Someone please can explain why this happen?

def order_sold

    if sold
         order = Order.find(params[:id])
       product = order.product

      if order
        if params[:status] == 'COMPLETED'
          order.status = Order.statuses[0]
          product = order.product
          quantity = product.quantity
          product.quantity -= order.quantity
          product.save
          transaction = Transaction.new
          transaction.user_id = order.buyer_id
          transaction.status = params[:status]
          order.transaction = transaction
          order.save
          OrderMailer.order_confirmation(order).deliver


        end
      end


    else
      logger.info("FAILED")
    end
    render nothing: true
  end

the logger:

[object Object]

/data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/database_statements.rb:371:in block in commit_transaction_records' /data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/database_statements.rb:370:in each' /data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/database_statements.rb:370:in commit_transaction_records' /data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/database_statements.rb:218:in transaction' /data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/transactions.rb:208:in transaction' /data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/transactions.rb:311:in with_transaction_returning_status' /data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/transactions.rb:259:in block in save' /data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/transactions.rb:270:in rollback_active_record_state!' /data/viop/releases/167/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/transactions.rb:258:in save' /data/viop/releases/167/app/controllers/orders_controller.rb:216:in order_sold

Aucun commentaire:

Enregistrer un commentaire