【发布时间】:2016-08-09 16:40:45
【问题描述】:
我开始构建一个应用程序,其中包括产品、购物车和付款。您首先通过转到 /products 将产品添加到您的购物车。然后,如果您导航到 /cart,系统将填充您准备结帐的产品列表。计划是将购物车表中的“总价”属性链接到付款表。
如何链接来自不同表的两个属性以使它们相同?我已经标记了需要相同的两个属性,“总价”和“金额”。
create_payments.rb
class CreatePayments < ActiveRecord::Migration
def change
create_table :payments do |t|
t.string :first_name
t.string :last_name
t.string :last4
***t.decimal :amount, precision: 12, scale: 3***
t.boolean :success
t.string :authorization_code
t.timestamps null: false
end
end
end
create_order_items.rb
class CreateOrderItems < ActiveRecord::Migration
def change
create_table :order_items do |t|
t.references :product, index: true, foreign_key: true
t.references :order, index: true, foreign_key: true
t.decimal :unit_price, precision: 12, scale: 3
t.integer :quantity
***t.decimal :total_price, precision: 12, scale: 3***
t.timestamps null: false
end
end
end
如果需要任何其他文件来帮助解决问题,请告诉我。提前感谢您提供任何类型的帮助!
【问题讨论】:
-
相同是什么意思?
-
我基本上想用 order_items 表中的“total_price”属性替换 payment 表中的“amount”属性。因此,当用户付款时,它会根据购买的产品填充欠款。
-
我认为您需要在您的
payments中添加一个belongs_to对您的Order的引用,并在您的付款中添加一个has_one。这样您就可以简单地获取所有OrderItems的价格并填充amount属性之类的。payment.amount = payment.order.order_items.select("total_price").reduce(&:+)
标签: ruby-on-rails ruby payment-gateway shopping-cart product