gpt4 book ai didi

ruby-on-rails - 如何找到一个对象并将其插入数据库

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:08 25 4
gpt4 key购买 nike

我正在尝试从购物车中获取 shop_id 和 subscription_id 并将它们插入/保存到购买数据库中。

我有一个购物车、line_items 和订阅。

这就是我尝试这样做的方式:

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

我得到了 shop_id,但由于某种原因 subscription_id 为 nil。

有什么想法可能是错误的吗?

更新 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

最佳答案

此处构建仅将一个新的 line_item 与@cart 链接起来,像这样,LineItem.new(cart_id: @cart.id)。 subscription_id 未在此处链接,因为@cart 与订阅没有关系。

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

获取所需的 subscription_id 并将其分配给“line_item.subscription_id”

line_item.subscription_id = fetch_the_subscription_id
line_item.save!

关于ruby-on-rails - 如何找到一个对象并将其插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44615656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com