Forum: Ruby on Rails Has and belongs to many, join table id included in query

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.
5ca17be18ed84b27dc0d9062216e2da8?d=identicon&s=25 Christian Johansen (chrisjoha)
on 2008-10-15 00:26
I'm using a habtm association for the first time and I'm having some
trouble when inserting several rows. The id of the join table seems to
be set to an already existing id causing the insert to fail.

class ClassA < ActiveRecord::Base
  has_and_belongs_to_many :bs,
                          :class_name => "ClassB",
                          :join_table => :abs

end

class ClassB < ActiveRecord::Base
  belongs_to :class_a
end

My join table migration:

  def self.up
    create_table :abs do |t|
      t.integer :class_a_id, :null => false
      t.integer :class_b_id, :null => false
    end

    add_index :abs, [:class_a_id, :class_b_id], :unique => true
  end

Then finally in the controller:

@a.bs = ClassB.find params[:bs]
@a.save

Now, this works the first time, but the second time I get this back:

Mysql::Error: #23000Duplicate entry '1' for key 1: INSERT INTO `abs`
(`class_a_id`, `id`, `class_b_id`) VALUES (7, 1, 1)

Now, I wonder - why is the id included in the query with a value that is
already used? I'm sure it's just something I'm doing wrong, but I'm
having a hard time figuring out what. I looked at the :insert_sql option
to habtm too, but don't know how to include the ids dynamically?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-15 00:48
(Received via mailing list)
On Oct 14, 11:26 pm, Christian Johansen <rails-mailing-l...@andreas-
s.net> wrote:
> I'm using a habtm association for the first time and I'm having some
> trouble when inserting several rows. The id of the join table seems to
> be set to an already existing id causing the insert to fail.
>
habtm join tables should not have an id column (use :id => false as an
option to create_table)

Fred
Ff168162d53e22788d576582b3527e97?d=identicon&s=25 Bill Kocik (Guest)
on 2008-10-15 01:53
(Received via mailing list)
Not to pimp my blog or anything, but I wrote about this exact issue
here: http://bkocik.net/2007/07/26/habtm-and-the-id-field/

That should help you understand what's going on (in a very long-winded
manner). :)

On Oct 14, 6:26 pm, Christian Johansen <rails-mailing-l...@andreas-
5ca17be18ed84b27dc0d9062216e2da8?d=identicon&s=25 Christian Johansen (chrisjoha)
on 2008-10-15 11:15
Bill Kocik wrote:
> Not to pimp my blog or anything, but I wrote about this exact issue
> here: http://bkocik.net/2007/07/26/habtm-and-the-id-field/
>
> That should help you understand what's going on (in a very long-winded
> manner). :)
>
> On Oct 14, 6:26�pm, Christian Johansen <rails-mailing-l...@andreas-

Ah, great stuff, thanks!
This topic is locked and can not be replied to.