# ID from child table not handled by AR

**URL:** <https://www.ruby-forum.com/t/id-from-child-table-not-handled-by-ar/94733>\
**Category:** Rails\
**Created:** [January 8, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/id-from-child-table-not-handled-by-ar/94733 "2006-01-08T14:50:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 8, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/id-from-child-table-not-handled-by-ar/94733/1 "2006-01-08T14:50:20Z")

</div>

Hi all,

Im slowly moving forward, but there’s something a miss. I can save some  
relational records to tables, but only once. For an unknown reason, the  
primary key (id) of the child table is not automatically increased. (I  
can  
only perform one save, (the first records id field holds the value ‘0’)  
and  
during the next save it says:

Mysql::Error: Duplicate entry ‘0’ for key 1: INSERT INTO  
project\_contacts …

The field that holds the foreign key (project\_id) is set correct btw.  
What’s  
eventually needed is to copy contact info from another contact table all  
together.

Some info:

## Models:

class Project \< ActiveRecord::Base  
has\_one :project\_contact  
end  
class ProjectContact \< ActiveRecord::Base  
belongs\_to :project  
end

## Controller Methods

def new\_project  
@project = Project.new

```
@contacts_list = Contact.find_by_sql('select t1.name, t2.firstname,

```

t2.lastname, t2.id from companies AS t1, contacts AS t2 where t1.id =  
t2.company\_id order by t1.name’)

```
## Showing a nice dropdown list with "Company - Fname Lname"
## Thanx to Chris H.
@contacts_list = @contacts_list.collect { |c| [ c.name + " - " + 

```

c.firstname

- " " + c.lastname ] }  
render\_action ‘edit\_project’  
end

def create\_project  
@project = Project.new(@params[‘project’])  
@project\_contact = ProjectContact.new(@params[‘id’])  
@project.project\_contact = @project\_contact

```
#@project.project_contact.save
if @project.save
	flash[:notice] = 'Project was successfully created.'
	redirect_to :action => 'show_project', :id => @project.id
else
	render :action => 'edit_project'
end

```

end

Gerard -hope-my-mails-dont-feel-like-spam-yet- Petersen

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 8, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/id-from-child-table-not-handled-by-ar/94733/2 "2006-01-08T15:04:04Z")

</div>

Does your database table definition indicate that the id field is  
AUTO\_INCREMENT ?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 8, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/id-from-child-table-not-handled-by-ar/94733/3 "2006-01-08T15:20:43Z")

</div>

I’m not worthy … oh master Kevin … 😉

You found the nails’ head. It’s just that I’m going through the Rails  
book,  
and all the online Ruby guides available (bookshop didn’t have the ruby  
book  
in stock at the moment).  
But as usual I’m expecting to much of myself. I want to know it all and  
am  
only working ruby on rails for under a month. Im in ‘zoomed in to  
much’-mode  
a bit to often … 🙂

Anyway thanx a million!!

On Sunday 08 January 2006 15:04, Kevin O. tried to type something  
like:

> Does your database table definition indicate that the id field is  
> AUTO\_INCREMENT ?

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 8, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/id-from-child-table-not-handled-by-ar/94733/4 "2006-01-08T15:41:57Z")

</div>

You can get a PDF version of the ‘programming ruby’ book from  
[www.pragmaticprogrammers.com](http://www.pragmaticprogrammers.com).

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 8, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/id-from-child-table-not-handled-by-ar/94733/5 "2006-01-08T17:03:43Z")

</div>

I know, thanx. But the book store put my name down so I got one in less  
then  
two weeks.

Regards,

Gerard.

On Sunday 08 January 2006 15:41, Kevin O. tried to type something  
like:

> You can get a PDF version of the ‘programming ruby’ book from  
> [www.pragmaticprogrammers.com](http://www.pragmaticprogrammers.com).

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!
