如何在 Rails 中为订单提供送货地址和帐单地址
在我的在线商店中,每个订单都与送货地址和帐单地址相关联(当然,它们可以相同)。 这是我第一次尝试对此进行建模:
Class Order
belongs_to :billing_address, :class => "Address"
belongs_to :shipping_address, :class => "Address"
效果很好,但现在表单助手不起作用。 即, form_for
只会生成名称类似于 address[zipcode]
的字段,因此我必须手动破解它才能获取 billing_address[zipcode]
和送货地址[邮政编码]
。
我想我可以使用单表继承将 Address
子类为 ShippingAddress
和 BillingAddress
,但这对我来说似乎有点老套(并且与一些好的东西相矛盾)答案在建模客户地址的最佳方法)。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要指定类名称,因为它不是 BillingAddress 或 ShippingAddress。
要完成关联:
You need to specify the class name, since it's not BillingAddress or ShippingAddress.
To complete the association:
我有两个想法给你,其中一个或两个都可以解决问题:
请用你的表单助手尝试一下,我很想知道它是否适合你。 希望能帮助到你!
I have two ideas for you, either or both of which may do the trick:
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!