2010-12-15 100 views
3

昨天我工作得很好,做了一些改变,我不知道我是如何破坏它的。我确定这是一个错别字,但我看不到它。在Mongoid中没有为references_one保存的嵌套属性

对窗体中的嵌套模型所做的任何更改根本就没有被保存,开发日志显示的是属性 - 而且它们看起来是正确的格式,但它根本没有更新。

我有一个User模型,references_one Biography像这样:

# app/models/user.rb 
class User 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    field :first_name, :type => String 
    field :last_name, :type => String 
    devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable 

    references_one :biography 

    accepts_nested_attributes_for :biography 
    #--snip--- 
end 

# app/models/biography.rb 
class Biography 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    field :content, :type => String 
    field :role, :type => String 
    field :is_crew, :type => Boolean 

    referenced_in :user 
end 

最后我的形式(我用简单的形式,在这里,但在大多数情况下它的表现非常相似,formtastic):

<%= simple_form_for [:manage, @user], :html => {:multipart => true}, :position => :left do |f| %> 
    <h2>Login details</h2> 
    <%= f.input :email, :input_html => {:class => "text"} %>  
    <%= f.input :first_name, :input_html => {:class => "text"} %> 
    <%= f.input :last_name, :input_html => {:class => "text"} %> 

    <div class="biography"> 
    <h2>Biography</h2> 
    <%= f.simple_fields_for :biography do |biography_form| %> 
     <%= biography_form.input :role, :input_html => {:class => "text"} %> 
     <%= biography_form.input :content, :as => :text, :input_html => {:class => "textarea"} %> 
     <%= biography_form.input :is_crew, :as => :boolean %> 
    <%- end -%> 
    </div> 

    <%= f.submit "Save user", :class => "submit mid" %> 
<% end %> 

而且从我的开发日志一些输出,柜面的答案躺在那里,我只是不能看到他们:

Started POST "/manage/users/john-doe" for 127.0.0.1 at Wed Dec 15 11:42:09 +1100 2010 
     Processing by Manage::UsersController#update as HTML 
     Parameters: {"commit"=>"Save user", "authenticity_token"=>"44QlHsbKb8Pm91wnxWJa8Y0QsUXDzp/3rVpfs3G1Inc=", "utf8"=>"✓", "id"=>"john-doe", "user"=>{"biography_attributes"=>{"is_crew"=>"0", "role"=>"Tenor", "id"=>"4d080de56a4f1dfe7700000e", "content"=>"John was born on the 1st of January, 1970."}, "last_name"=>"Doe", "first_name"=>"Johnathan", "email"=>"[email protected]"}} 
    the_idea_of_north_development['users'].find({:_id=>BSON::ObjectId('4d06e6036a4f1dcb1b000001')}, {}).limit(-1) 
    the_idea_of_north_development['users'].find({:slug=>"john-doe"}, {}).limit(-1) 
    the_idea_of_north_development['biographies'].find({"user_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {}).limit(-1) 
    the_idea_of_north_development['$cmd'].find({"count"=>"users", "query"=>{:_id=>{"$ne"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, :email=>/^[email protected]\.com\.au$/i}, "fields"=>nil}, {}).limit(-1) 
    the_idea_of_north_development['users'].find({"slug"=>"johnathan-doe"}, {}) 
    MONGODB the_idea_of_north_development['users'].update({"_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {"$set"=>{"slug"=>"johnathan-doe", "updated_at"=>Wed Dec 15 00:42:09 UTC 2010, "first_name"=>"Johnathan"}}) 
    Redirected to http://lvh.me:3000/manage/users 
    Completed 302 Found in 17ms 

User型号的任何更改都可以更新,但对Biography的更改未保存。帮助我蜂巢心,你是我唯一的希望!

回答

0

我一直有一个类似的问题,嵌套的文件没有被保留的变化。 (问题是How Do I Update Nested Mongo Document Attributes in Rails with Mongoid?)。我怀疑Mongoid本身的,因为这样的消息下面是从未登录更新嵌入文档:如果尝试正在取得作出更新到嵌套文件

MONGODB the_idea_of_north_development['users'].update({"_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {"$set"=>{"slug"=>"johnathan-doe", "updated_at"=>Wed Dec 15 00:42:09 UTC 2010, "first_name"=>"Johnathan"}}) 

,会有一个行类似于一个选择父文件和设置嵌套文档的值,如:

the_idea_of_north_development['users'].update({"_id"=>PARENT_DOCUMENT}, {"$set"=>{"biography_attributes.is_crew"=>"0", "biography_attribute.role"=>"Tenor", etc...}) 

就个人而言,我觉得Mongoid Issue #357一个可能的原因。

+0

看起来问题#357可能会在mongoid 2.0 rc 1中解决,预计在今年1月份的某个时候。 – wwilkins 2010-12-20 23:40:57

+0

感谢您的跟进。我一直在研究这个问题,我认为你可能是正确的,如果补丁分类出来,一定会接受这个答案。伙计们继续存在。 – theTRON 2010-12-21 04:30:22

+0

刚刚更新到RC1,看起来像问题#357仍然没有修复,它引入了一系列引用模型的新问题。结束不得不回到beta-20。 – theTRON 2011-01-10 02:58:59

0

在同一问题上两周内死亡。 我设法做的唯一修复如下。 在用户控制器,这样做:

if @user.save 
params[:user][:biography_attributes].each do |bio| 
     @biography = @user.biography.build({ :role => bio[:role], :iscrew => bio[:iscrew] }) 
    end 

你会遍历传记的所有实例,eplicitly它保存到数据库中。