如何在 Rails 中为订单提供送货地址和帐单地址

发布于 2024-07-16 18:27:04 字数 629 浏览 11 评论 0原文

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

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

效果很好,但现在表单助手不起作用。 即, form_for 只会生成名称类似于 address[zipcode] 的字段,因此我必须手动破解它才能获取 billing_address[zipcode]送货地址[邮政编码]

我想我可以使用单表继承将 Address 子类为 ShippingAddressBillingAddress,但这对我来说似乎有点老套(并且与一些好的东西相矛盾)答案在建模客户地址的最佳方法)。

In my online store, each order is associated with a shipping address and a billing address (they can be the same, of course). This is my first attempt to model this:

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

This works pretty well, but now the form helpers don't work. I.e., form_for will only generate fields with names like address[zipcode], so I have to manually hack it to get billing_address[zipcode] and shipping_address[zipcode].

I guess I could use single table inheritance to subclass Address into ShippingAddress and BillingAddress, but this seems a bit hacky to me (and contradicts some good answers in Best way to model Customer <--> Address).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极致的悲 2024-07-23 18:27:04

您需要指定类名称,因为它不是 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

You need to specify the class name, since it's not BillingAddress or 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

To complete the association:

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
蔚蓝源自深海 2024-07-23 18:27:04

我有两个想法给你,其中一个或两个都可以解决问题:

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

Class Order
  belongs_to :address, :foreign_key => "billing_address_id"
  belongs_to :address, :foreign_key => "shipping_address_id"

请用你的表单助手尝试一下,我很想知道它是否适合你。 希望能帮助到你!

I have two ideas for you, either or both of which may do the trick:

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

Class Order
  belongs_to :address, :foreign_key => "billing_address_id"
  belongs_to :address, :foreign_key => "shipping_address_id"

Please give them a try with your form helpers and I'd be interested to know if it works out for you. Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文