ruby-on-railsrubymongoidmongoid4

Mongoid::Errors::InvalidScope with no aparent reason


I'm using mongoid-4.0.0.beta1 and following their docs to define a scope I get this error:

/usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta1/lib/mongoid/scopable.rb:306:in `check_scope_validity':  (Mongoid::Errors::InvalidScope)
Problem:
  translation missing: pt-BR.mongoid.errors.messages.invalid_scope.message
Summary:
  translation missing: pt-BR.mongoid.errors.messages.invalid_scope.summary
Resolution:
  translation missing: pt-BR.mongoid.errors.messages.invalid_scope.resolution
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta1/lib/mongoid/scopable.rb:90:in `default_scope'
    from /Users/aelesbao/src/getninjas/kitana/app/models/subscription.rb:21:in `<class:Subscription>'
    from /Users/aelesbao/src/getninjas/kitana/app/models/subscription.rb:3:in `<top (required)>'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:229:in `require'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:229:in `block in require'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:229:in `require'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:330:in `require_or_load'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:463:in `load_missing_constant'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:184:in `const_missing'
    from /Users/aelesbao/src/getninjas/kitana/spec/models/subscription_spec.rb:3:in `<top (required)>'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `block in load'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

Sample:

class Band
  include Mongoid::Document

  field :name, type: String
  field :active, type: Boolean, default: true

  default_scope where(active: true)
end

Solution

  • Actually, it seems that from version 4.0 onward we need to use lambda for scopes:

    class Band
      include Mongoid::Document
    
      field :name, type: String
      field :active, type: Boolean, default: true
    
      default_scope -> { where(active: true) }
    end