具有多个不同类型地址的订单 [英] Order having multiple addresses of different kinds

查看:47
本文介绍了具有多个不同类型地址的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有订单和地址的模型,并希望订单的每个实例都有地址表中的送货地址和账单地址.我正在尝试找出设置此功能的最佳方法,但无法使其正常工作.

I have models for Order and Address and would like each instance of Order to have a shipping and billing address from the Address table. I am trying to figure out the best way to set this up but can't get it to work.

我试过了:

class Address < ActiveRecord::Base
  belongs_to :order
end

has_one :billing_address, -> { where type: 'billing' }, class_name: "Address"
has_one :shipping_address, -> { where type: 'shipping' }, class_name: "Address"

这不起作用,但希望应该让我了解我正在尝试做什么.任何指导将不胜感激.

which does not work but hopefully should give a sense of what I'm trying to do. Any guidance would be greatly appreciated.

推荐答案

有以下几种方法 -

方法 1: 使用内置的 STI(单表继承)机制.

Approach 1: Using builtin STI (Single Table Inheritance) mechanism.

class Order < ActiveRecord::Base
  has_one :billing_address
  has_one :shipping_address
end

class Address < ActiveRecord::Base
  belongs_to :order

  # make sure that there is an `order_id` field 
  # and `type` field on `addresses` table for STI
end

class BillingAddress < Address    
end

class ShippingAddress < Address    
end

示例:

order = Order.new

billing_address = BillingAddress.new(content: '123 doggy lane')
order.billing_addresss = billing_address

shipping_address = ShippingAddress.new(content: '123 doggy lane')
order.shipping_address = shipping_address

order.save!

方法 2: 使用自定义 STI(单表继承)机制.

Approach 2: Using custom STI (Single Table Inheritance) mechanism.

class Order < ActiveRecord::Base
  has_one :billing_address
  has_one :shipping_address
end

class Address < ActiveRecord::Base
  belongs_to :order

  # make sure that there is an `order_id` field 
  # and `address_type` field on `addresses` table for custom STI
  # important note: make sure its `address_type`, not `type`
  # if it is `type`, rails builtin STI kicks in

  validates_inclusion_of :address_type, in: ['shipping', 'billing']
end

示例:

order = Order.new

billing_address = Address.new(address_type: 'billing', content: '123 doggy lane')
order.billing_addresss = billing_address

shipping_address = Address.new(address_type: 'shipping', content: '123 doggy lane')
order.shipping_address = shipping_address

order.save!

方法 3: 按照 order 存储地址信息,而不是相反.

Approach 3: Storing the address information in the order, instead of the other way around.

class Order < ActiveRecord::Base
  belongs_to :billing_address, class_name: 'Address', foreign_key: 'billing_address_id'
  belongs_to :shipping_address, class_name: 'Address', foreign_key: 'shipping_address_id'

  # make sure that there are `billing_address_id` and `shipping_address_id` 
  # fields setup on the `orders` table
end   

class Address < ActiveRecord::Base
  has_many :billable_orders, class_name: 'Order', foreign_key: 'billing_address_id'
  has_many :shippable_orders, class_name: 'Order', foreign_key: 'shipping_address_id'
end

示例:

order = Order.new

billing_address = Address.new(content: '123 doggy lane')
order.billing_addresss = billing_address

shipping_address = Address.new(content: '123 doggy lane')
order.shipping_address = shipping_address

order.save!

这篇关于具有多个不同类型地址的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆