2011-09-15 43 views
0

我不知道發生了什麼,但似乎無法找到一小段代碼。我使用了沒有嵌入式文檔的mongomappper,一切都很順利。使用嵌入式文檔,我撞到了磚牆。Mongomapper:嵌入式文檔無法正常工作

這裏是我的模型:

class Assessment 
    include Mongomapper::Document 
    many :sections 
end 

class Section 
    include MongoMapper::EmbeddedDocument 
    key :title, String 
    validates_presence_of :title 
end 

當我運行:
a = Assessment.create(:sections => [ Section.new(:title => 'test') ])

我會得到下面的輸出:

我使用Rails的控制檯下的 mongomapper homepage的說明

=> #<Assessment _id: BSON::ObjectId('4e71efce69a74c0fb6000002'), sections: nil>

我也試過a = Assessment.new(),a.sections << Section.new(:title => "test")
但結果相同。

爲什麼內部沒有內容?

+0

什麼版本的Mongo?什麼版本的MongoMapper?我認爲MongoMapper和Mongo 2.0並沒有在一起發揮出色。請參閱:http://groups.google.com/group/mongomapper/browse_thread/thread/8aab1abe6a0b348d?hl=en%3Fhl%3Den –

+1

我檢查了我的版本,我運行的是mongodb 2.0.0,mongomapper 0.9.2,Rails 3.0 .10,Ruby 1.9.2p290。 我卸載了mongo並安裝了1.8.3版本。我犯了同樣的錯誤。 – jackie

+0

我想我只是對數據的顯示方式感到困惑。 mm 0.9.1不會打印'sections:nil',但是即使內部有部分,0.9.2也是如此...... – jackie

回答

1

包括:

attr_accessible :sections 

應使創建工作(在它爲我)。並且您可能需要添加:

validates_associated :sections 

使您的評估驗證您預期的嵌入式部分。

是的,晚了幾年,但我只是要解決這個問題,谷歌帶我到這裏。

相關問題