mercredi 21 juin 2017

How to find a object and insert it in a database

I’m trying to pick up the shop_id and subscription_id from a cart and insert/save them into the purchase database.

I have a cart, line_items and subscriptions.

And this is how I’m trying to do this:

@purchase = @cart.line_items.build 
@purchase = current_shop.purchases.build(purchase_params)
@purchase.save!

I got the shop_id, but the subscription_id for some reason is nil.

Any ideas what maybe wrong?

Update 1

def purchase_params
  params.permit(:subscription_id, :shop_id, :created_at, :updated_at, :id, :cart_id)
end


Started POST "/line_items?subscription_id=1" for ::1 at 2017-06-18 16:45:12 +0300
Processing by LineItemsController#create as HTML
Parameters:  {"authenticity_token"=>"dUonc4AnCvFTuK1b+TAKho/kmpvl7XaOM7SGcNalzdQV1+CqhY4 p7znDiL/TV12pVKeDTqlR7j5NL65X1S/75A==", "subscription_id"=>"1"}
Cart Load (0.1ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" =   ? LIMIT ?  [["id", 14], ["LIMIT", 1]]
(0.1ms)  begin transaction
SQL (0.4ms)  INSERT INTO "carts" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", 2017-06-18 13:45:12 UTC], ["updated_at", 2017-06-18 13:45:12 UTC]]
 (0.7ms)  commit transaction
 Subscription Load (0.2ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  (0.1ms)  begin transaction
 SQL (0.6ms)  INSERT INTO "line_items" ("subscription_id", "cart_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["subscription_id", 1], ["cart_id", 18], ["created_at", 2017-06-18 13:45:12 UTC], ["updated_at", 2017-06-18 13:45:12 UTC]]
 (0.9ms)  commit transaction
Redirected to http://localhost:3000/carts/18
Completed 302 Found in 21ms (ActiveRecord: 3.2ms)

I'm implementing braintree payments via the following create method and this is where I have the small peace of code:

def create
  current_shop.has_payment_info?
    @result = Braintree::Transaction.sale(
            amount: @cart.total_price,
            payment_method_nonce: params[:payment_method_nonce],
            customer: {
              first_name: current_shop.first_name,
              last_name: current_shop.last_name,
              company: current_shop.shop_name,
              email: current_shop.email,
              phone: current_shop.phone_number,
              website: current_shop.web_page
            },
            options: { store_in_vault: true })


      if @result.success?
    current_shop.update(braintree_customer_id:    @result.transaction.customer_details.id) unless  current_shop.has_payment_info?


    @purchase = @cart.line_items.build 
    @purchase.save!
    @purchase = current_shop.purchases.build(purchase_params)
    @purchase.save!
    @cart.destroy
    redirect_to front_index_path, notice: 'Your transaction was succesfully processed'
  else
gon.client_token = generate_client_token
redirect_to :back, :notice => 'Something went wrong while processing your transaction. Please try again!'
  end
end

Update 2

class Purchase < ApplicationRecord
    belongs_to :shop
    belongs_to :cart
end

class Shop < ApplicationRecord
  has_many :items
  has_many :purchases
  has_many :subscriptions
end


class Subscription < ApplicationRecord
    has_many :line_items
    belongs_to :shop
end

class Cart < ApplicationRecord
  has_many :line_items, dependent: :destroy
  has_many :purchases
end

class LineItem < ApplicationRecord
  belongs_to :subscription
  belongs_to :cart
end

Aucun commentaire:

Enregistrer un commentaire