【发布时间】:2014-09-09 13:08:46
【问题描述】:
我知道我做错了,但我看不出在哪里。 我有这两个模型:
Subscription.rb(子)
class Subscription < ActiveRecord::Base
attr_accessible :state, :subscriber_id, :subscriber_type, :last_payment
belongs_to :subscriber, polymorphic: true
validates :subscriber_id, presence: true
validates :subscriber_type, presence: true
end
restorer.rb(父)
class Restorer < User
attr_accessible :firstname, :lastname, :restaurant_attributes, :subscription_attributes
has_one :restaurant, dependent: :destroy, :autosave => true
has_one :subscription, as: :subscriber, :autosave => true
accepts_nested_attributes_for :restaurant
accepts_nested_attributes_for :subscription
end
当我想要两个创建一个新的恢复器和一个新的订阅时(同时) 它不起作用:
def create
@restorer = Restorer.create params[:restorer]
@restaurant = @restorer.build_restaurant params[:restorer][:restaurant_attributes]
@subscription = @restorer.build_subscription params[:restorer][:subscription_attributes]
if @restorer.save
...
else
...
end
end
【问题讨论】:
-
如果你使用
save你应该替换create到new,你也不需要手动使用build_..(它应该自动工作)并尝试save!这会引发消息如果有问题。 -
它引发了一个record_invalid错误...似乎有这个错误'subscriber_id should not be blank'
-
这是因为您无法验证
nested_attributes是否存在id,因为它们本质上几乎是在asynchronously创建的,没有id将在保存时出现,直到保存完成后。如果您真的必须验证这一点,而我认为您不需要使用嵌套属性,那么您将不得不在一个步骤中创建这些对象,例如创建订阅者然后创建关联。您也许可以在交易中处理此问题,但我对此没有什么经验。 -
好的,如何验证孩子的存在?
-
我可以解释这一点,但 This Blog Post 比我在 SO 上做的更简洁。
标签: ruby-on-rails ruby belongs-to has-one