我不确定我的设置有什么问题...我无法创建mongoid记录(使用mongoid 3.0.0):
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 :(得分:6)
解决方案是从ruby 1.9.2升级到ruby 1.9.3。提示来自http://mongoid.org/en/moped/,事实是moped
gem会引发错误。
答案 1 :(得分:0)
尝试
field :username, type: String