Forum: Ruby on Rails foreign key problem - 'key not found' error

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.
036be8d5d83a98e546b98d64e3fd6c20?d=identicon&s=25 Sam Donaldson (Guest)
on 2006-05-23 23:01
(Received via mailing list)
Hello,

I have the following structure:

model foo
  has_many :bars:,
                  :dependent => true
end

model bar
  belongs_to :foo
end

-----------------
I'm doing the following to insert the Foo record and it's dependent Bar
record:

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[...])

Any help on this would be appreciated.  I've been on this bug for 2 days
now.  Thanks.
This topic is locked and can not be replied to.