How to find an object and insert it into the database

I am trying to pick up shop_id and subscription_id from cart and insert / save them into shopping database.

I have a cart, line_items and subscriptions.

And this is how I try to do it:

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

      

I have a shop_id, but for some reason the subscription_id is null.

Any ideas what could be 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 am implementing Braintree payments with the following create method, and this is where I have my little code world:

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

      

+3


source to share


1 answer


Create here just links to the new line_item with @cart, for example LineItem.new (cart_id: @ cart.id). subscription_id is unrelated here as @cart is not related to subscriptions.

@cart.line_items.build 
line_item = @cart.line_items.build

      



select the desired subscription type and assign it to 'line_item.subscription_id'

line_item.subscription_id = fetch_the_subscription_id
line_item.save!

      

0


source







All Articles