Alvin Engler
2005-Sep-02 18:23 UTC
join table - undesired single quotes with varchar/string as primary key
Hello,
I''m integrating a rails application redesign with a legacy schema that
requires
the use of mixed character data as the primary key. I''m trying to
setup my
categories model to associate products which are related by a
has_and_belongs_to_many association.
(apologies if this comes through as a re-post for some)
The code below is what I''m using to do this. It works fine, except that
when it
inserts a product_id into the categories_products join table, it does so (so I
know all that is set up properly) but with single quotes around it like this:
''724359778709'' when the key''s proper varchar value is
724359778709. Thus the
association is not recognized after the insert because the product_id value in
products_categories does not equal the real product ID. When I manually insert
values into the join table, they are recognized and work fine.
def add_product
category = Category.find(params[:id])
new_product = Product.find(params[:input][:product_id])
if new_product
flash[:notice] = params[:input][:product_id] + " added"
category.products << new_product
end
redirect_to :action => ''edit'', :id =>
params[:id]
end
The above code generates the following SQL:
INSERT INTO categories_products (`product_id`, `category_id`) VALUES
(''\''724359778709\'''', ''1'')
Can anyone tell me what to do so that it does not place those escaped single
quotes in the INSERT... I''ve checked and all my MySQL fields match
(i.e. they
are all varchars of the proper length).
{update -- I was able to do proper inserts using custom SQL via :insert_sql in
the model definition, but this leaves other things broken, such as
push_with_attributes)
Thanks,
A. Engler