2010-12-15 73 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它保存到數據庫中。