ID from child table not handled by AR


#1

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!


#2

Does your database table definition indicate that the id field is
AUTO_INCREMENT ?


#3

I’m not worthy … oh master Kevin … :wink:

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 … :slight_smile:

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!


#4

You can get a PDF version of the ‘programming ruby’ book from
www.pragmaticprogrammers.com.


#5

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.


“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!