Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid::Errors::DocumentNotFound raise_not_found_error

* updated at the bottom *

When looking for user that doesnt exist, I am getting:

Mongoid::Errors::DocumentNotFound in UsersController#show

Problem: Document(s) not found for class User with id(s) 22. Summary: When calling User.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 22 ... (1 total) and the following ids were not found: 22. Resolution: Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.

However I am setting raise_not_found_error to false

mongoid.yml

development:
  adapter: 'mongoid'
  # 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: blog_development
      # Provides the hosts the default session can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017
      options:
          allow_dynamic_fields: false
          identity_map_enabled: true
          include_root_in_json: true
          include_type_for_serialization: true
          # Note this can also be true if you want to preload everything, but this is
          # almost never necessary. Most of the time set this to false.
          preload_models:
            - Canvas
            - Browser
            - Firefox
          scope_overwrite_exception: true
          raise_not_found_error: false
          skip_version_check: false
          use_activesupport_time_zone: false
          use_utc: true
  # Configure Mongoid specific options. (optional)
  options:
    # Enable the identity map, needed for eager loading. (default: false)
    # identity_map_enabled: false

    # Includes the root model name in json serialization. (default: false)
    # include_root_in_json: false

    # Include the _type field in serializaion. (default: false)
    # include_type_for_serialization: false

    # Preload all models in development, needed when models use
    # inheritance. (default: false)
    # preload_models: false

    # Protect id and type from mass assignment. (default: true)
    # protect_sensitive_fields: true

    # Raise an error when performing a #find and the document is not found.
    # (default: true)
    raise_not_found_error: false

    # Raise an error when defining a scope with the same name as an
    # existing method. (default: false)
    scope_overwrite_exception: false

    # Skip the database version check, used when connecting to a db without
    # admin access. (default: false)
    # skip_version_check: false

    # Use Active Support's time zone in conversions. (default: true)
    # use_activesupport_time_zone: true

    # Ensure all times are UTC in the app side. (default: false)
    # use_utc: false
test:
  sessions:
    default:
      database: blog_test
      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

Controller

  # GET /users/1
  # GET /users/1.json
  def show

    @user = User.find(params[:id])

    render json: @user
  end

* UPDATE ** fixed a null response (not in a json format) going out by doing this:

  def show

    @user = User.find(params[:id])
    if @user.nil?
      @user = []
    end

    render json: @user
  end
like image 386
azz0r Avatar asked Sep 05 '13 14:09

azz0r


3 Answers

your structure of yml is wrong

has to be -

development:
  sessions:
    options:
      #raise_not_found_error has to be not here but see below
  options: #strictly 2 spaces before
    raise_not_found_error: false #strictly 4 spaces before not 6

so, raise_not_found_error parameter has to be child of development>options, not development>sessions>options

like image 124
okliv Avatar answered Nov 19 '22 23:11

okliv


For me even correct indenting didn't work, what did is to create an initializer file called mongoid.rb in config/initializers/ and put this into it

Mongoid.raise_not_found_error = false
like image 40
rimkashox Avatar answered Nov 19 '22 23:11

rimkashox


To save someone few minutes, if you are still having a problem and you are sure your mongoid.yml configs are correct, try to stop spring server as it seems to do a lot of caching!

$ spring stop

like image 1
Waheed Avatar answered Nov 19 '22 21:11

Waheed