开发者

Mongomapper: Embedded document does not work properly

I don't know what's going on but I can't seem to find working a very small piece of code. I used mongomappper without embedded documents and everything went fine. Using an embedded document I hit the brick wall. I'm following the instructions on the mongomapper homepage using the Rails console:

Here are my models:

class Assessment
  include Mongomapper::Document
  many :sections
end

clas开发者_StackOverflow社区s Section
  include MongoMapper::EmbeddedDocument
  key :title, String
  validates_presence_of :title
end

When I run:

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

I will get the following output:

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

I also tried with a = Assessment.new(), a.sections << Section.new(:title => "test")

but the same result.

Why is there nothing inside sections?


Including:

attr_accessible :sections

should make the create work (at it does for me). And you'll probably want to add:

validates_associated :sections

to make your Assessment validate the embedded Sections the way you'd probably expect.

Yeah, a couple years late but I just had to sort this problem out and Google brought me here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜