Mongoid:在Rails控制台中使用embeded_in对象显示整个mongodb对象

时间:2011-07-17 19:07:18

标签: ruby-on-rails ruby ruby-on-rails-3 mongodb mongoid

class Parent
  include Mongoid::Document
  embeds_many :children
  field :title
end

class Child
  include Mongoid::Document
  embedded_in :parent
  field :name
end

Rails控制台

parent = Parent.new(:title => "Hello World")
parent.children << Child.new(:name => "Pedro")
parent
#=> #<Parent _id: 4e2330286254cc0e7d000007, _type: nil, title: "Hellow World">

那么如何在Rails控制台中检查整个对象,直到将孩子嵌入我的parent中,就像我可以在mogodb控制台中一样

{
  "_id" : ObjectId("4e2330286254cc0e7d000007"),
  "title": "Hello World",
  "children" : [
    {
      "_id" : ObjectId("4d3ed089fb60ab534684b7e0"),
      "name" : "Pedro"
    }
  ]
}

1 个答案:

答案 0 :(得分:6)

您可以尝试检查Mongoid对象的属性,如下所示:

parent.attributes.inspect