我有两个模型:
class Customer < ActiveRecord::Base
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :customer
validates :customer, presence: true
end
如果我执行以下操作,我会收到验证错误:
$ customer = Customer.new()
$ order = Order.new()
$ customer.orders << order
$ order.save!
为什么这会导致以下验证错误:
验证失败:订单无效
如果我改为保存客户:
$ customer = Customer.new()
$ order = Order.new()
$ customer.orders << order
$ customer.save!
我得到错误:
验证失败:客户不能为空
到底是怎么回事?我不应该验证belongs_to
关系吗?