无法使用Rails创建MongoDB文档

时间:2011-09-03 21:05:20

标签: ruby-on-rails mongodb mongoid

我在使用MongoDB和Rails时遇到了很多问题。我正在尝试提供用户提要,但是当我尝试保存文档时,Rails一直在侮辱我。我不知道发生了什么,文档似乎没有帮助(据我所知,我已经完成了本书的所有内容)。

以下是我所做的关系:

class Feed
  include Mongoid::Document
  field       :name,        :type => String
  field       :description, :type => String
  has_many    :feedposts
  embedded_in :user
end

class Feedpost
  include Mongoid::Document
  field :title    => String
  field :text     => String
  field :resource => String
  field :time     => Time, :default => -> { Time.now }
  belongs_to  :feed
  embeds_many :comments
end

class Comment
  include Mongoid::Document
  has_one :user
  field   :text => String
  field   :time => Time, :default => -> { Time.now }
  embedded_in :feedpost
end

当尝试保存Feed时(没有Feedpost或暗示的评论),它只是告诉我方法“new?”没有为NilClass定义。 我从Rails获得了这个回溯:

activesupport (3.1.0.rc5) lib/active_support/whiny_nil.rb:48:in `method_missing'
mongoid (2.2.0) lib/mongoid/persistence/operations/embedded/insert.rb:31:in `block in persist'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:26:in `block (3 levels) in prepare'
activesupport (3.1.0.rc5) lib/active_support/callbacks.rb:390:in `_run_create_callbacks'
activesupport (3.1.0.rc5) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:25:in `block (2 levels) in prepare'
activesupport (3.1.0.rc5) lib/active_support/callbacks.rb:390:in `_run_save_callbacks'
activesupport (3.1.0.rc5) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:24:in `block in prepare'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:22:in `tap'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:22:in `prepare'
mongoid (2.2.0) lib/mongoid/persistence/operations/embedded/insert.rb:30:in `persist'
mongoid (2.2.0) lib/mongoid/persistence.rb:44:in `insert'
mongoid (2.2.0) lib/mongoid/persistence.rb:149:in `upsert'

编辑: 这就是控制器:

class FeedsController < ApplicationController
  def index
    @user  = CurrentUser()
    @feeds = @user.feeds
  end

  def create
    @feed   = Feed.new params[:feed]
    success = @feed.save # This is the line that trigger the error
    render json: { :success => success }
  end
end

这是Mongoid的配置:

development:
  host: localhost
  database: noosphere_development

到底发生了什么事?有人有想法吗?

0 个答案:

没有答案