“p data”和“data.to_json”之间有什么区别

时间:2011-12-20 11:13:52

标签: sinatra mongoid activemodel

环境:

        
  • ubuntu     
  • sinatra     
  • mongoid

我的问题是: 我有博客,发布模型如:

class Blog
  include Mongoid::Document
  fields ~
  has_many :posts
end

class Post
  include Mongoid::Document
  fields ~
  belongs_to :blog
end

然后我在Rakefile中尝试这个

task :my_test do
  ENV['RACK_ENV'] = 'development'
  require "api.rb" #it will require all models、..etc    

  blog = Blog.where(urlname: "testblog").first

  ** p blog ** #this will output only blog attributes without posts
  ** p JSON.parse(blog.to_json) ** #this will output blog blog attrs and all it's post
end

我不知道是什么导致他们之间出现这种差异


感谢

0 个答案:

没有答案