我正在尝试从购物车中取出shop_id和subscription_id,然后将其插入/保存到购买数据库中。
我有购物车,line_items和订阅。
这就是我试图这样做的方式:
@purchase = @cart.line_items.build
@purchase = current_shop.purchases.build(purchase_params)
@purchase.save!
我收到了shop_id,但由于某种原因,subscription_id为零。
任何想法可能有什么不对?
更新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)
我通过以下创建方法实施braintree付款,这是我拥有小代码的地方:
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
更新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
答案 0 :(得分:0)
这里的构建只链接一个新的line_item和@cart,就像这样,LineItem.new(cart_id:@ cart.id)。由于@cart与订阅没有关系,因此subscription_id未链接。
@cart.line_items.build
line_item = @cart.line_items.build
获取所需的subscription_id并将其分配给&#39; line_item.subscription_id&#39;
line_item.subscription_id = fetch_the_subscription_id
line_item.save!