Forum: Ruby on Rails habtm problem

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Little K. (Guest)
on 2009-02-02 22:58
I have Kids and I have Sports.

I have a checkbox list of Sports for each Kid.

Let's say I make a Kid, and assign him to all 5 sports.

It works, great.

Let's say I make another Kid(Kid2), and assign him to all 5 sports. I
get this error:

Mysql::Error: Duplicate entry '2' for key 1: INSERT INTO kids_sports
(`sport_id`, `kid_id`, `id`) VALUES (2, 10, 2)

And my kids_sports table looks like this:

                       id    kid_id  sport_id
    Edit     Delete  2    8    2
  Edit   Delete   3   8   3
  Edit   Delete   4   8   4
  Edit   Delete   6   8   6
  Edit   Delete   7   8   7

Why the heck is it doing that? Why isn't id 1,2,3,4,5 etc like it
should(?) be?
Freddy A. (Guest)
on 2009-02-02 23:37
(Received via mailing list)
I think we need to see one your kid/sport model and then the new/
create method in the controller...

You should not have a id column btw in the kids_sports table.. .

This should be your up task:

  def self.up
    create_table :kids_sports, :id => false do |t|
      t.integer :kid_id
      t.integer :sport_id
    add_index :kids_sports, [:kid_id]
    add_index :kids_sports, [:sport_id]

On Feb 2, 12:58 pm, Little K. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.