gpt4 book ai didi

ruby-on-rails - 如何在 rails 中给订单送货地址和账单地址

转载 作者:行者123 更新时间:2023-12-04 06:22:52 24 4
gpt4 key购买 nike

在我的在线商店中,每个订单都与一个送货地址和一个账单地址相关联(当然,它们可以相同)。这是我第一次尝试对此建模:

Class Order
belongs_to :billing_address, :class => "Address"
belongs_to :shipping_address, :class => "Address"

这工作得很好,但现在表单助手不工作了。即,form_for 只会生成名称如 address[zipcode] 的字段,所以我必须手动破解它才能获得 billing_address[zipcode]送货地址[邮政编码]

我想我可以使用单表继承将 Address 子类化为 ShippingAddressBillingAddress,但这对我来说似乎有点老套(而且与 Best way to model Customer <--> Address 中的一些好的答案相矛盾。

最佳答案

您需要指定类名,因为它不是 BillingAddress 或 ShippingAddress。

class Order < ActiveRecord::Base
# foreign key not required here because it will look for
# association_name_id, e.g. billing_address_id, shipping_address_id
belongs_to :billing_address, :class_name => "Address"
belongs_to :shipping_address, :class_name => "Address"
end

完成关联:

class Address < ActiveRecord::Base
# foreign key required here because it will look for class_name_id,
# e.g. address_id
has_many :billing_orders, :class_name => "Order",
:foreign_key => "billing_address_id"
has_many :shipping_orders, :class_name => "Order",
:foreign_key => "shipping_address_id"
end

关于ruby-on-rails - 如何在 rails 中给订单送货地址和账单地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/726468/

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