Background: I am running MS SQL server with a legacy DB schema overlaid by AR-friendly views; the only thing odd about the views themselves is that the id in one table is nvarchar, not integer (it's used as the foreign key by all the other legacy tables).
When adding a has_and_belongs_to_many relationship which uses this varchar key, the SQL statement generated has three quotes on either side of the id , eg
INSERT INTO bears_frogs ([bear_id], [frog_id]) VALUES (TEDDY_13,'1');
The quotes are generated in the code which creates the SQL itself (I verified this by playing around extensively with the console). It seems to be line #128-129 of the file C:\ruby\lib\ruby\gems\1.8\gems\activerecord-1.10.1\lib\active_record\associations\has_and_belongs_to_many_association.rb which causes the problem.
I only observed this with my wacko legacy schema running on MS SQL.