# 'key not found' problem with saving model object

**URL:** <https://www.ruby-forum.com/t/key-not-found-problem-with-saving-model-object/95152>\
**Category:** Rails\
**Created:** [May 23, 2006, 9:32am UTC](https://www.ruby-forum.com/t/key-not-found-problem-with-saving-model-object/95152 "2006-05-23T09:32:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 23, 2006, 9:32am UTC](https://www.ruby-forum.com/t/key-not-found-problem-with-saving-model-object/95152/1 "2006-05-23T09:32:40Z")

</div>

Hello,

I have the following structure:

model foo  
has\_many :bars:,  
:dependent =\> true  
end

model bar  
belongs\_to :foo  
end

In my action, when i try to save foo:

def save  
begin  
f = foo.new  
f.a = 5

```
begin
  b = bar.new
  b.a = 10
  f.bars << b
rescue Exception => exc
  ...
end

f.save! <-- This does not happen successfully, and I end up in 

```

the  
exception handler with ‘key not found’  
rescue Exception =\> exc  
…  
end  
end

Would anybody know what’s going wrong here? Do I need to save f first?

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 23, 2006, 10:03am UTC](https://www.ruby-forum.com/t/key-not-found-problem-with-saving-model-object/95152/2 "2006-05-23T10:03:47Z")

</div>

I’m guessing you have foreign keys defined?

You won’t be able to save a Bar until it’s associated Foo has been  
saved. Also, you probably need to add the Bar to the Foo after the Foo  
has been saved so that the foreign key on the Bar is set correctly.

When you are trying to save f, it tries to save the associated Bar  
objects first, which will fail because their foreign key is not set.  
You probably need to do something like…

ActiveRecord::Base.transaction do  
f = Foo.create(:a =\> 10)  
f.bars.create(:a =\> 10)  
end

-Jonathan.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 23, 2006, 10:37am UTC](https://www.ruby-forum.com/t/key-not-found-problem-with-saving-model-object/95152/3 "2006-05-23T10:37:49Z")

</div>

Do I have to start a transaction since we know bar can only be saved if  
f is  
saved. I mean, say for example f for some reason does not validate and  
is  
therefore not saved, will bar still be saved in the table?

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 23, 2006, 11:05am UTC](https://www.ruby-forum.com/t/key-not-found-problem-with-saving-model-object/95152/4 "2006-05-23T11:05:30Z")

</div>

If the intention of the transaction was to remove the f from the  
Foo.createif something went wrong then it seems like it doesn’t work  
because i’m able  
to successfully insert a row for foo while bar errored out.

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 23, 2006, 11:30am UTC](https://www.ruby-forum.com/t/key-not-found-problem-with-saving-model-object/95152/5 "2006-05-23T11:30:21Z")

</div>

Tried the following:

f = Foo.create(…)

This successfully creates and stores f away in the table.

Now,

b = Bar.new(params[…])  
f.bars \<\< b \<-- This should set the foreign key in b automatically.

But I get a ‘key not found’ problem. I get the same problem if I do:

f.bars.create(params[…])

Thanks for everything.
