# Unit Tests crashing out for a table without an ID

**URL:** <https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297>\
**Category:** Rails\
**Created:** [June 2, 2006, 1:29pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297 "2006-06-02T13:29:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 2, 2006, 1:29pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297/1 "2006-06-02T13:29:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 7, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297/2 "2006-06-07T18:08:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 7, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297/3 "2006-06-07T18:29:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 7, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297/4 "2006-06-07T19:26:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 7, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297/5 "2006-06-07T19:35:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 8, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297/6 "2006-06-08T16:15:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 8, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297/7 "2006-06-08T16:21:15Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 8, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/unit-tests-crashing-out-for-a-table-without-an-id/62297/8 "2006-06-08T19:51:52Z")

</div>

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
