2012-09-28 50 views
1

我有两个模型,我正在使用事件和报告。报告并嵌入到事件中。我无法为特定活动创建新报告。Mongoid在父内创建新的嵌入式文档

我觉得我的遥控器新的行动必须是这个样子:

@event = Event.find(params[:eventid]) 
@report = @event.report.build 

在我的事件模型中,我有以下设置:

embeds_one :report 
accepts_nested_attributes_for :report 

当我尝试保存我收到以下错误:

Mongoid::Errors::NoParent 

这是我的报告模型

class Report 
include Mongoid::Document 
include Mongoid::Timestamps 
field :test, type: String 
embedded_in :event, :inverse_of => :report 
embeds_many :report_details 
accepts_nested_attributes_for :report_details, 
    :allow_destroy => true, 
    :reject_if => proc { |attributes| 
     attributes['name'].blank? && attributes['_destroy'].blank? 
    } 

这里是我的事件模型

class Event 
include Mongoid::Document 
include Mongoid::Timestamps 
embeds_one :report 
accepts_nested_attributes_for :report, 
:allow_destroy => true, 
    :reject_if => proc { |attributes| 
    attributes['name'].blank? && attributes['_destroy'].blank? 
    } 

在此先感谢。

+0

请给你的模型结构 – abhas

回答

4

确保有event_id组上你的新报告,当你创建它。

您可以通过使用@report = @event.report.build(params[:report])你暗示,或确保“事项标识”被包含在params哈希表做到这一点。

+0

感谢您的帮助:) –

+0

没问题!如果解决了这个问题,你会介意接受答案吗? – ccurtisj