Although I have an entry in the database, it gives "Mongoid :: Errors :: DocumentNotFound"

Although I have an entry with id 13163 ( db.locations.find({_id: 13163})

) it gives me an error:

Mongoid :: Errors :: DocumentNotFound in LocationsController # show

Problem: Document not found for class Location with id (s) 13163. Description: When calling Location.find with an id or an array of id's, each parameter must match a document in the database or this error will be raised. The search was for id (s): 13163 ... (1 total) and the following IDs were not found: 13163. Resolution: Search for an id that is in the database or Mongoid.raise_not_found_error config parameter is set to false, which will return zero instead of raising this error when looking for a single identifier, or only matched documents when looking for multiples.

# Use callbacks to share common setup or constraints between actions.
def set_location
  @location = Location.find(params[:id])
end

      

locations_controller.rb:

class LocationsController < ApplicationController
  before_action :set_location, only: [:show, :edit, :update, :destroy]

  # GET /locations
  # GET /locations.json
  def index
    @locations = Location.all
  end

  # GET /locations/1
  # GET /locations/1.json
  def show
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_location
      @location = Location.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def location_params
      params.require(:location).permit(:loc_name_en, :loc_name_jp, :channel)
    end
end

      

Setting the option raise_not_found_error: false

does not work like this as I have a document in the database.


DECISION:

Many thanks @mu is too short to give me a hint.

The problem can be solved in two ways:

  • Declare field :_id, type: Integer

    in modellocation.rb

  • Or converting pass parameter to Integer as Location.find(params[:id].to_i)

    in locations_controller.rb

    as shown below in @mu's too short answer
+3


source to share


1 answer


I am assuming you have a type problem. You say it is:

db.locations.find({_id: 13163})

      

finds document in MongoDB wrapper. This means that you have a document in the collection locations

, _id

- a number 13163

. If you used the line '13163

':

db.locations.find({_id: '13163'})

      

you will not find your document. The value in params[:id]

is probably a string, so you say:

Location.find('13163')

      

when you want to say:



Location.find(13163)

      

If _id

indeed is a number, you need to make sure you call find

with a number:

Location.find(params[:id].to_i)

      

You are probably confused because sometimes Mongoid will convert between String

and Moped::BSON::ObjectId

(and sometimes it won't), so if yours _id

is a regular ObjectId, you can say:

Model.find('5016cd8b30f1b95cb300004d')

      

and Mongoid will convert this string to ObjectId for you. Mongoid does not convert String

to number for you, you have to do it yourself.

+3


source







All Articles