Forum: Ruby on Rails Unit Tests crashing out for a table without an ID

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9757acb5b356cbf87576c2005f2cf33c?d=identicon&s=25 Ben Moxon (glenatron)
on 2006-06-02 13:29
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?
9757acb5b356cbf87576c2005f2cf33c?d=identicon&s=25 Ben Moxon (glenatron)
on 2006-06-07 18:08
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?
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-06-07 18:29
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.
37a786a2425b62259869a86adf5219bb?d=identicon&s=25 Forrest Chang (Guest)
on 2006-06-07 19:26
(Received via mailing list)
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
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2006-06-07 19:35
(Received via mailing list)
What database are you using for your test database?
Have you loaded all fixtures in your test?
9757acb5b356cbf87576c2005f2cf33c?d=identicon&s=25 Ben Moxon (glenatron)
on 2006-06-08 16:15
Brian Hogan 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.
9757acb5b356cbf87576c2005f2cf33c?d=identicon&s=25 Ben Moxon (glenatron)
on 2006-06-08 16:21
Alex Wayne 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...
37a786a2425b62259869a86adf5219bb?d=identicon&s=25 Forrest Chang (Guest)
on 2006-06-08 19:51
(Received via mailing list)
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
This topic is locked and can not be replied to.