Forum: Ruby on Rails ID from child table not handled by AR

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.
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2006-01-08 14:50
(Received via mailing list)
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 Hall
	@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!
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-08 15:04
Does your database table definition indicate that the id field is
AUTO_INCREMENT ?
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2006-01-08 15:20
(Received via mailing list)
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 Olbrich 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!
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-08 15:41
You can get a PDF version of the 'programming ruby' book from
www.pragmaticprogrammers.com.
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2006-01-08 17:03
(Received via mailing list)
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 Olbrich 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!
This topic is locked and can not be replied to.