Askar
Askar

Reputation: 5854

Though I have a record in database, it's giving "Mongoid::Errors::DocumentNotFound"

Though I have the record with id 13163 (db.locations.find({_id: 13163})), it's giving me error:

Mongoid::Errors::DocumentNotFound in LocationsController#show

Problem: Document(s) not found for class Location with id(s) 13163. Summary: When calling Location.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): 13163 ... (1 total) and the following ids were not found: 13163. 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.

# 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 up the option raise_not_found_error: false is not the case as I do have a document in database.


SOLUTION:

Big thanks to @mu is too short for giving me a hint.

The problem can be solved in 2 ways:

  1. Declare field :_id, type: Integer in the model location.rb
  2. Or converting the passing parameter to Integer like Location.find(params[:id].to_i) in locations_controller.rb as shown below in the @mu is too short's answer

Upvotes: 3

Views: 1138

Answers (1)

mu is too short
mu is too short

Reputation: 434805

I'd guess that you have a type problem. You say that this:

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

finds the document in the MongoDB shell. That means that you have a document in the locations collection whose _id is the number 13163. If you used the string '13163':

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

you won't find your document. The value in params[:id] is probably a string so you're saying:

Location.find('13163')

when you want to say:

Location.find(13163)

If the _id really is a number then you'll need to make sure you call find with a number:

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

You're probably being confused because sometimes Mongoid will convert between Strings and Moped::BSON::ObjectIds (and sometimes it won't) so if your _id is the usual ObjectId you can say:

Model.find('5016cd8b30f1b95cb300004d')

and Mongoid will convert that string to an ObjectId for you. Mongoid won't convert a String to a number for you, you have to do that yourself.

Upvotes: 3

Related Questions