Unit Tests crashing out for a table without an ID

I’m having problems unit testing my model- I have a kind of quiz
functionality user table and a question table and a
user_question_response table that contains the id for the user, the id
for the question and the user’s response. The user_question_response has
it’s own model belongs_to/has_many with users and questions and it works
fine in the actual application but when I try to do any unit-testing of
my User model I get this:

ActiveRecord::RecordNotFound: Couldn’t find UserQuestionResponse without
an ID
…/gems/activerecord_1.14.2/lib/active_record/base.rb:932:in
‘find_from_ids’

Any ideas what is going on and what I can do about it?

ben moxon wrote:

I’m having problems unit testing my model- I have a kind of quiz
functionality user table and a question table and a
user_question_response table that contains the id for the user, the id
for the question and the user’s response. The user_question_response has
it’s own model belongs_to/has_many with users and questions and it works
fine in the actual application but when I try to do any unit-testing of
my User model I get this:

ActiveRecord::RecordNotFound: Couldn’t find UserQuestionResponse without
an ID
…/gems/activerecord_1.14.2/lib/active_record/base.rb:932:in
‘find_from_ids’

Any ideas what is going on and what I can do about it?

I still haven’t found an explanation for this- I’m figuring it’s a
problem with the current version of Rails, but I don’t understand the
internals of the platform enough to be able to resolve it.

Does anyone have any working unit tests involving a table without an id
field?

ben moxon wrote:

ActiveRecord::RecordNotFound: Couldn’t find UserQuestionResponse without
an ID
…/gems/activerecord_1.14.2/lib/active_record/base.rb:932:in
‘find_from_ids’

Is that as far back as the stack trace goes? Take it back all the way
to see if at any point you are executing app code rather than framework
code.

Also make sure your fixtures are correct, and not trying to insert ids
where they shouldnt.

I’ve gotten this error when I’ve accidentally passed nil as the id
(due to some processing error that sets the variable passed to find()
)

You might check that the id you passed is legit

Forrest

What database are you using for your test database?
Have you loaded all fixtures in your test?

Brian H. wrote:

What database are you using for your test database?
Have you loaded all fixtures in your test?

Using MySQL, 4.current - the error comes during the fixture loading part

  • the fixture itself I generated directly from my development database,
    where it is working fine, so I know the data works when the application
    is up and running.

The table is a classic annotated link so it has user_id, question_id and
question_response - no normal id field because there will only ever be
one response for any combination of user_id and question_id. I’m pretty
sure this is the root of the problem but I don’t understand why it only
comes up during testing.

Alex W. wrote:

ben moxon wrote:

ActiveRecord::RecordNotFound: Couldn’t find UserQuestionResponse without
an ID
…/gems/activerecord_1.14.2/lib/active_record/base.rb:932:in
‘find_from_ids’

Is that as far back as the stack trace goes? Take it back all the way
to see if at any point you are executing app code rather than framework
code.

Also make sure your fixtures are correct, and not trying to insert ids
where they shouldnt.

That is the top of the stack trace. It begins with
…/gems/activerecord_1.14.2/lib/active_record/fixtures.rb:547:in
‘setup’

My code doesn’t appear in the stack trace at all, which is why I don’t
really know where to start fixing it.

It’s like ActiveRecord is trying to perform an UPDATE but doesn’t know
what to do it with because it can’t find the ID field. I don’t know why
it would be doing that, though…

Like I said, I got the same error when I accidently set the id to nil,
which is why it wouldn’t be found. Print out what the id is that goes
into the find to see.

Forrest