Cherruel, Gildas
2005-Aug-15 12:11 UTC
RE: has_many_belongs_to_many insert issues [CLOSED]
Found the issue. In the association table (categories.products) it is very important to use lowercase column names. I.e, product_id and category_id. I had Product_id and Category_id in SQL.... Gildas -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Cherruel, Gildas Sent: Monday, August 15, 2005 1:13 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] has_many_belongs_to_many insert issues Hi, I have an hmbtm relationship setup like: class Catogory < ActiveRecord::Base has_many_belongs_to_many: products end class Product < ActiveRecord::Base has_many_belongs_to_many: categories end With the usual SQL tables (i.e., categories, products, categories_products). NOTE: I AM USING MICROSOFT SQL SERVER -- that might be the source of my issues... All my ids are int identity(1, 1) not null in SQL. I quit using uniqueidentifier as Rails gets very confused. I cannot add a Product<->Category relationship via Rails, like: aProduct = Product.find(41234) P.categories << aCategory It throws an exception coming from SQL: Execute OLE error code:80040E14 in Microsoft OLE DB Provider for SQL Server Line 1: Incorrect syntax near '')''. HRESULT error code:0x80020009 Exception occurred.: INSERT INTO partners_recipients () VALUES () While it is obvious the SQL statement fails, I don''t understand why Rails generated this buggy statement. If I had the relationship by hand in SQL, I can perfectly issue a find in Rails or retrieve it.>From the source code, it looks like:File: activerecord-1.11.1.1962\lib\active_record\associations\has_and_belongs_ to_many_association.rb Method: insert_record Lines 121 and 123 seem to work as I get something in the breakpointer. And I guess line 139, since I cannot make the @owner.send work: "INSERT INTO #{@join_table} (#{@owner.send(:quoted_column_names, attributes).join('', '')}) " + "VALUES (#{attributes.values.join('', '')})" Fails. More specifically: @owner.send(:quoted_column_names, attributes)>From my debug in irb, it sounds like attribute contains Strings (atleast in my case) and they don''t accept a :quoted_column_names method, at least I can''t find that method. Am I missing a patch, or something?!? Thanks, Gildas _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails