使用 rails4.2.5 创建 rails 工程 rails new test_mongo
修改 gemfile
-gem 'sqlite3' +gem 'mongoid', git: 'git://github.com/mongoid/mongoid.git'
删除默认 config/database.yml 文件
创建 mongid 配置文件 rails g mongid:config
启动 rails console rails c
报错:
gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/application/configuration.rb:110:in `database_configuration': Cannot load `Rails.application.database_configuration`: (RuntimeError)
Could not load database configuration. No such file - ["config/database.yml"]
@huacnlee @qinfanpeng @lb563 感谢,搞定了!
1.
-gem 'sqlite3'
+gem 'mongoid', git: 'git://github.com/mongoid/mongoid.git'
2.
bundle install
rails g mongid:config
-require 'rails/all'
+require 'action_controller/railtie'
+require 'action_mailer/railtie'
+require 'rails/test_unit/railtie'
+require 'sprockets/railtie'
4. 注释:
config/application.rb
/# config.active_record.raise_in_transactional_callbacks = true
enviroments/development.rb
/# config.active_record.migration_error = :page_load
enviroments/production.rb
/# config.active_record.dump_schema_after_migration = false
rails g model teacher name:string
rails c
Teacher.count
报错如下: DEBUG -- : MONGODB | xxxxx:27018 | test_development.count | FAILED | comparison of Fixnum with nil failed | 0.008398999999999998s ArgumentError: comparison of Fixnum with nil failed
我自己本地的 db.version 是 3.0.7 , 项目中没有升级过,是 2.0.6
> db.version()
3.0.7
>
> db.col.insert({title: 'MongoDB 教程',
... description: 'MongoDB 是一个 Nosql 数据库',
... by: '菜鸟教程',
... url: 'http://www.runoob.com',
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted" : 1 })
> db.version()
2.0.6
> db.col.insert({title: 'MongoDB 教程',
... description: 'MongoDB 是一个 Nosql 数据库',
... by: '菜鸟教程',
... url: 'http://www.runoob.com',
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
Cannot use commands write mode, degrading to compatibility mode
WriteResult({ "nInserted" : 1 })
通过命令行插入时,会有一个警告,但也会插入成功,如果使用 rails console 就会报错,不知道是否跟 mongo server 的版本有关 ,升级了 rails 版本到 4.2.5,和 mongoid 到 5.0.0,但是 mongo server 的版本是 2.0.6 不知道是否跟 mongo server 的版本有关 ruby china 在升级的时候,有没有遇到这个问题?
log
ArgumentError: comparison of Fixnum with nil failed
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/protocol/message.rb:117:in `>'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/protocol/message.rb:117:in `deserialize'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/server/connectable.rb:107:in `block in read'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/server/connectable.rb:91:in `ensure_connected'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/server/connectable.rb:106:in `read'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/server/connection.rb:180:in `deliver'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/server/connection.rb:125:in `block in dispatch'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/monitoring/publishable.rb:47:in `publish_command'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/server/connection.rb:124:in `dispatch'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/operation/executable.rb:36:in `block in execute'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/server/connection_pool.rb:108:in `with_connection'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/server/context.rb:63:in `with_connection'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/operation/executable.rb:34:in `execute'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/database.rb:158:in `command'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/collection/view/readable.rb:136:in `block in count'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/retryable.rb:51:in `call'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/retryable.rb:51:in `read_with_retry'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongo-2.2.1/lib/mongo/collection/view/readable.rb:135:in `count'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongoid-5.0.0/lib/mongoid/contextual/mongo.rb:59:in `block in count'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongoid-5.0.0/lib/mongoid/contextual/mongo.rb:489:in `try_cache'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongoid-5.0.0/lib/mongoid/contextual/mongo.rb:59:in `count'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongoid-5.0.0/lib/mongoid/contextual.rb:20:in `count'
from /Users/abc/.rvm/gems/ruby-2.2.1/gems/mongoid-5.0.0/lib/mongoid/findable.rb:54:in `count'