2012-07-10 81 views
0

我不知道什么是错我的设置......我不能创建一个mongoid记录(使用mongoid 3.0.0):Mongoid不能创造纪录

bundle exec rails console

ruby-1.9.2-p290 :045 > u = NoUser.new 
=> #<NoUser _id: 4ffc2e8487bc11b128000005, _type: nil, username: nil> 
ruby-1.9.2-p290 :046 > u.username = 'aaa1' 
=> "aaa1" 
ruby-1.9.2-p290 :047 > u 
=> #<NoUser _id: 4ffc2e8487bc11b128000005, _type: nil, username: "aaa1"> 
ruby-1.9.2-p290 :048 > u.save 
NoMethodError: You have a nil object when you didn't expect it! 
You might have expected an instance of Array. 
The error occurred while evaluating nil.[] 
    gems/moped-1.1.1/lib/moped/node.rb:74:in `block in command' 
    gems/moped-1.1.1/lib/moped/node.rb:522:in `[]' 
    gems/moped-1.1.1/lib/moped/node.rb:522:in `block (3 levels) in flush' 
    gems/moped-1.1.1/lib/moped/node.rb:521:in `map' 
    gems/moped-1.1.1/lib/moped/node.rb:521:in `block (2 levels) in flush' 
    gems/moped-1.1.1/lib/moped/node.rb:113:in `ensure_connected' 
    gems/moped-1.1.1/lib/moped/node.rb:517:in `block in flush' 
    gems/moped-1.1.1/lib/moped/node.rb:532:in `logging' 
    gems/moped-1.1.1/lib/moped/node.rb:516:in `flush' 
    gems/moped-1.1.1/lib/moped/node.rb:505:in `process' 
    gems/moped-1.1.1/lib/moped/node.rb:70:in `command' 
    gems/moped-1.1.1/lib/moped/node.rb:356:in `refresh' 
    gems/moped-1.1.1/lib/moped/cluster.rb:101:in `block in refresh' 
    gems/moped-1.1.1/lib/moped/cluster.rb:114:in `each' 
    gems/moped-1.1.1/lib/moped/cluster.rb:114:in `refresh' 
    gems/moped-1.1.1/lib/moped/cluster.rb:67:in `nodes' 
... 9 levels... 
    gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks' 
    gems/mongoid-3.0.0/lib/mongoid/callbacks.rb:98:in `run_callbacks' 
    gems/mongoid-3.0.0/lib/mongoid/persistence/insertion.rb:24:in `block in prepare' 
    gems/activesupport-3.1.0/lib/active_support/callbacks.rb:390:in `_run_save_callbacks' 
    gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks' 
    gems/mongoid-3.0.0/lib/mongoid/callbacks.rb:98:in `run_callbacks' 
    gems/mongoid-3.0.0/lib/mongoid/persistence/insertion.rb:23:in `prepare' 
    gems/mongoid-3.0.0/lib/mongoid/persistence/operations/insert.rb:26:in `persist' 
    gems/mongoid-3.0.0/lib/mongoid/persistence.rb:50:in `insert' 
    gems/mongoid-3.0.0/lib/mongoid/persistence.rb:79:in `save' 
    from (irb):48 
    gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start' 
    gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start' 
    gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>' 
    from script/rails:6:in `require' 
    from script/rails:6:in `<main>'ruby-1.9.2-p290 :049 > 

;

class NoUser 

    include Mongoid::Document 

    field :username, :type => String 
end 
+0

恩,我的模特怎么了?看起来像,我保证! – 2012-07-10 13:49:00

+0

检查相同的操作是否适用于您项目中的其他收藏 – abhas 2012-07-10 13:55:06

回答

6

解决的办法是从红宝石1.9.2升级到1.9.3红宝石。暗示来自http://mongoid.org/en/moped/,事实上它是moped gem会抛出错误。

+0

为我工作! – rafaelkin 2012-07-13 21:48:33

0

尝试

field :username, type: String