Munou Diagram 2

Let's munou!

(HABTM) Has_and_belongs_to_many Bug in Ruby on Rails

| Comments

Once the HABTM relation was made, for example:

class Song < ActiveRecord::Base
  has_and_belongs_to_many :artists
end
The operation shown below is valid:
song.artists << Artist.new(:name => 'Barbra Streisand')
But this may make a BIG problem:
song.artists << Artist.find(:name => 'Barbra Streisand')

Mysql::Error: Duplicate entry ‘1’ for key 1: INSERT INTO artists_songs (`artist_id`, `id`, `song_id`) VALUES (1, 1, 3) The id field should be “autoincrement”. Why pass a Fixnum to it? That is the question. I find out that when we push(<<) an artist to a song, whole the attributes on this artist will be expanded as arguments of the SQL query. And then duplicate :id to :artist_id.

Hum, it sounds great. There is no problem. But the :id key-value pair is still. That means we will establish a HABTM relation with a given key. So we sometimes get error from here. Try the tricky way:

class Song < ActiveRecord::Base
  has_and_belongs_to_many :artists,
  :insert_sql => 'INSERT INTO `artists_songs` ( `song_id`, `artist_id` )
                  VALUES (#{id}, #{record.id})'
end
How awful the code!

Comments