Topic: How to resolve "couldn't find 'model-name' with id=" error

My app runs fine if the nested resource has a record associating the parent resource, otherwise I get the error:

couldn't find QuestionResponse with id=2 [WHERE "question_responses"."question_id" = 2]

Not sure what is going wrong, but I made two demo apps in the same way (except that they used scaffolding) and they worked fine even if their was no child resource record associating to the parent resource, and I did need to put any condition.

In this app, I tried to put condition:

!@question.question_responses.nil?

but the condition fails and the error shifts to the condition itself.

I have posted the code on Gist here: https://gist.github.com/2412177

Re: How to resolve "couldn't find 'model-name' with id=" error

I see that rails is trying to find QuestionResponse with id = 2 which is id of Question but not QuestionResponse.

Check Your code at:

def find_question_response
   @question_response = @question.question_responses.find(params[:id])
  end

You are passing id of Question not QuestionResponse that's why it cannot be found.

Regarding condition:

!@question.question_responses.nil?

may fail if collection is not nil but empty. Better use:

@question.question_responses.length > 0

or

@question.question_responses.blank?

which checks if object is false, empty, nil or a whitespace string.

Re: How to resolve "couldn't find 'model-name' with id=" error

olhor wrote:

I see that rails is trying to find QuestionResponse with id = 2 which is id of Question but not QuestionResponse.

Check Your code at:

def find_question_response
   @question_response = @question.question_responses.find(params[:id])
  end

You are passing id of Question not QuestionResponse that's why it cannot be found.

That cannot be the problem. That is finding by scope. If I change it to say, @QuestionResponse.find(), then I won't be finding question_response in scope of question. This is exactly what the books also suggest.

Regarding condition:

!@question.question_responses.nil?

may fail if collection is not nil but empty. Better use:

@question.question_responses.length > 0

or

@question.question_responses.blank?

which checks if object is false, empty, nil or a whitespace string.

I have tried these methods as well. But, going by how I did demo apps, I shouldn't need to apply these conditions at all.

Last edited by vandamon (2012-04-20 09:59:38)

Re: How to resolve "couldn't find 'model-name' with id=" error

vandamon wrote:

That cannot be the problem. That is finding by scope. If I change it to say, @QuestionResponse.find(), then I won't be finding question_response in scope of question. This is exactly what the books also suggest.

I understand this is scoped but try passing something like params[:question][:question_response_id].
By default params[:id] in QuestionsController refers to id of a Question but not a QuestionResponse.

Re: How to resolve "couldn't find 'model-name' with id=" error

olhor wrote:
vandamon wrote:

That cannot be the problem. That is finding by scope. If I change it to say, @QuestionResponse.find(), then I won't be finding question_response in scope of question. This is exactly what the books also suggest.

I understand this is scoped but try passing something like params[:question][:question_response_id].
By default params[:id] in QuestionsController refers to id of a Question but not a QuestionResponse.

@question_response = @question.question_responses.find(params[:question][:question_id])

gives error

undefined method `[]' for nil:NilClass (NoMethodError)

And @question_response = @question.question_responses.find(params[:question_id])   

gives

Couldn't find QuestionResponse without an ID (ActiveRecord::RecordNotFound)

Re: How to resolve "couldn't find 'model-name' with id=" error

Please list all of parameters passed to this controller when getting those errors.

Re: How to resolve "couldn't find 'model-name' with id=" error

olhor wrote:

Please list all of parameters passed to this controller when getting those errors.

Question and its ID. The errors occur when I call show on listed questions.

Re: How to resolve "couldn't find 'model-name' with id=" error

When You request 'show' action You get:
1) before_filter :find_question creates new @question instance
2) before_filter :find_question_response tries to find responses for @question using 'id' of question but not 'id' of question_repsonse (so here is the error thrown)
Look:

def find_question_response
  @question_response = @question.question_responses.find(params[:id])
end

and change this to:

@question_responses = @question.question_responses.all

to get all responses for this question
3) 'show' action overwrites this @question variable with it's own new instance: @question = Question.find(params[:id]). Using such logic You can get different instance of @question from the one made in point 1) (this is also bad)

Re: How to resolve "couldn't find 'model-name' with id=" error

@question_responses = @question.question_responses.all

throws error: undefined method `model_name' for NilClass

Not sure why

@question_response = @question.question_responses.find(params[:id])

would work perfectly with other apps, with exactly the same things (a parent and a child resource), and not with this app? There is no logical structural difference between these apps.