2

Rails アプリケーションを ec2 インスタンスにデプロイしました。使用するデータベースは Mongo で、別のインスタンスでホストされています。それに応じて、mongoid.yml のデータベースとホストが変更され、db サーバーを指します。アプリケーションは本番モードで実行されており、正常に動作しています。本番モードでRailsコンソールにアクセスすると、

           $rails c RAILS_ENV=production

モデル User.last へのアクセスを試みます。エラーが発生します

    Loading RAILS_ENV=production environment (Rails 3.2.11)
    1.9.3p327 :001 > User.last
    Mongoid::Errors::NoSessionsConfig: 
    Problem:
      No sessions configuration provided.
    Summary:
      Mongoid's configuration requires that you provide details about each session that can be connected to, and requires in the sessions config at least 1 default session to exist.
    Resolution:
      Double check your mongoid.yml to make sure that you have a top-level sessions key with at least 1 default session configuration for it. You can regenerate a new mongoid.yml for assistance via `rails g mongoid:config`.

     Example:
       development:
         sessions:
           default:
             database: mongoid_dev
             hosts:
               - localhost:27017


        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions/factory.rb:61:in `create_session'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions/factory.rb:43:in `default'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions.rb:109:in `default'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions.rb:378:in `__session__'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions.rb:213:in `mongo_session'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions.rb:157:in `collection'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual/mongo.rb:216:in `initialize'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual.rb:47:in `new'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual.rb:47:in `create_context'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual.rb:30:in `context'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual.rb:18:in `last'
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/finders.rb:129:in `last'
        from (irb):1
        from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'

Mongoid.yml

デフォルト: &defaults ホスト: localhost

        development:
          # Configure available database sessions. (required)
          sessions:
            # Defines the default session. (required)
            default:
              # Defines the name of the default database that Mongoid can connect to.
              # (required).
              database: app_db
              # Provides the hosts the default session can connect to. Must be an array
              # of host:port pairs. (required)
              hosts:
            - localhost:27017
              options:

          options:

        test:
          sessions:
            default:
              database: app_db
              hosts:
            - localhost:27017
              options:
            consistency: :strong
            # In the test environment we lower the retries and retry interval to
            # low amounts for fast failures.
            max_retries: 1
            retry_interval: 0


        production:
          # Configure available database sessions. (required)
          sessions:
            # Defines the default session. (required)
            default:
              # Defines the name of the default database that Mongoid can connect to.
              # (required).
              database: app_db    
              # Provides the hosts the default session can connect to. Must be an array
              # of host:port pairs. (required)
              hosts:
            - ec2-**-***-***-*.ap-southeast-1.compute.amazonaws.com:27017
              options:

        options:
4

1 に答える 1

1

試してみてください

RAILS_ENV=production rails c

または@Rubymanが提案したように

rails c production

あなたのコマンドの問題は、モンゴイドが呼ばれる環境を探していることですRAILS_ENV=production

于 2013-06-28T10:14:58.963 に答える