Rails Rails 4.2.5 + Mongid 5.0.0 启动问题!

michael_roshen · 2016年01月05日 · 最后由 michael_roshen 回复于 2016年01月06日 · 3368 次阅读

使用 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"]
1 楼 已删除

看下你的 model 是是否还是用的 ActiveRecord::Base,看起来还是用的 ActiveRecord

需要移除一些默认的 railtie

@huacnlee @qinfanpeng @lb563 感谢,搞定了!

1.

-gem 'sqlite3'
+gem 'mongoid', git: 'git://github.com/mongoid/mongoid.git'

2.

bundle install 
rails g mongid:config
  1. config/application.rb
-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

#5 楼 @michael_roshen 我感觉第 4 步不是必须的喃,当然最好注了。

#6 楼 @qinfanpeng 不注释不行,不 require active_record 找不到 active_record 方法

@huacnlee @lb563

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'
需要 登录 后方可回复, 如果你还没有账号请 注册新账号