Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 具有多个不同类型地址的订单_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 具有多个不同类型地址的订单

Ruby on rails 具有多个不同类型地址的订单,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有订单和地址的模型,希望每个订单实例都有地址表中的发货和账单地址。我试图找出最好的方法来设置它,但无法让它工作 我试过: class Address < ActiveRecord::Base belongs_to :order end has_one :billing_address, -> { where type: 'billing' }, class_name: "Address" has_one :shipping_address, -> { where typ

我有订单和地址的模型,希望每个订单实例都有地址表中的发货和账单地址。我试图找出最好的方法来设置它,但无法让它工作

我试过:

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"
类地址{where type:'billing'},类名称:“address”
有一个:配送地址,->{where type:'shipping'},类名称:“address”

这不起作用,但希望能让我感觉到我在努力做什么。任何指导都将不胜感激。

以下是如何满足您的要求

迁移

rails g migration CreateOrders
rails g migration CreateAddresses shipping_id:integer billing_id:integer content:string
rake db:migrate
class Order < ActiveRecord::Base
  has_one :shipping_address, class_name: 'Address', foreign_key: 'shipping_id'
  has_one :billing_address, class_name: 'Address', foreign_key: 'billing_id'
end

class Address < ActiveRecord::Base
  belongs_to :order
end
课程

rails g migration CreateOrders
rails g migration CreateAddresses shipping_id:integer billing_id:integer content:string
rake db:migrate
class Order < ActiveRecord::Base
  has_one :shipping_address, class_name: 'Address', foreign_key: 'shipping_id'
  has_one :billing_address, class_name: 'Address', foreign_key: 'billing_id'
end

class Address < ActiveRecord::Base
  belongs_to :order
end
类顺序
实施

rails c

order = Order.create
address = Address.create(content: '123 doggy lane')
order.shipping_address = address
order.billing_address = address
order.save!

order.shipping_address
#=> #<Address id: 1, shipping_id: 1, billing_id: 1, content: "123 doggy lane">
order.billing_address
#=> #<Address id: 1, shipping_id: 1, billing_id: 1, content: "123 doggy lane">
rails c
order=order.create
地址=地址。创建(内容:“123狗巷”)
order.shipping\u address=地址
order.billing_address=地址
命令,救命!
订单发货地址
#=> #
order.billing\u地址
#=> #

以下是如何满足您的要求

迁移

rails g migration CreateOrders
rails g migration CreateAddresses shipping_id:integer billing_id:integer content:string
rake db:migrate
class Order < ActiveRecord::Base
  has_one :shipping_address, class_name: 'Address', foreign_key: 'shipping_id'
  has_one :billing_address, class_name: 'Address', foreign_key: 'billing_id'
end

class Address < ActiveRecord::Base
  belongs_to :order
end
课程

rails g migration CreateOrders
rails g migration CreateAddresses shipping_id:integer billing_id:integer content:string
rake db:migrate
class Order < ActiveRecord::Base
  has_one :shipping_address, class_name: 'Address', foreign_key: 'shipping_id'
  has_one :billing_address, class_name: 'Address', foreign_key: 'billing_id'
end

class Address < ActiveRecord::Base
  belongs_to :order
end
类顺序
实施

rails c

order = Order.create
address = Address.create(content: '123 doggy lane')
order.shipping_address = address
order.billing_address = address
order.save!

order.shipping_address
#=> #<Address id: 1, shipping_id: 1, billing_id: 1, content: "123 doggy lane">
order.billing_address
#=> #<Address id: 1, shipping_id: 1, billing_id: 1, content: "123 doggy lane">
rails c
order=order.create
地址=地址。创建(内容:“123狗巷”)
order.shipping\u address=地址
order.billing_address=地址
命令,救命!
订单发货地址
#=> #
order.billing\u地址
#=> #

有多种方法可以实现这一点,如下所示-

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

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
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
方法2:使用自定义STI(单表继承)机制

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
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
方法3:
顺序存储地址信息,而不是相反的方式

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

有多种方法可以实现这一点,如下所示-

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

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
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
方法2:使用自定义STI(单表继承)机制

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
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
方法3:
顺序存储地址信息,而不是相反的方式

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

方法是正确的,但不要将
类型
用于列名,它是为STI保留的。您可以更改模型的默认STI列,但是我更喜欢使用列名称,如
address\u type
context
。此外,对于未来,请避免使用像
这样的句子,因为它不起作用,而不解释它是如何起作用的-异常和不当行为之间有巨大的区别。这种方法是正确的,但不要对你的列名使用
类型
,它是为STI保留的。您可以更改模型的默认STI列,但是我更喜欢使用列名称,如
address\u type
context
。此外,对于未来,请避免使用像
这样的句子,因为它不起作用,而不解释它是如何起作用的-异常和不当行为之间存在巨大差异。它是否也处理每个模型的多个地址。例如,一个用户可以有多个帐单地址。或者该代码必须更新吗?它是否也处理每个型号的多个地址。例如,一个用户可以有多个帐单地址。还是必须更新此代码?