Skip to content
Browse files

Ensure that primary_keys of HABTM records is not double quoted

[#5152 state:reslved]
  • Loading branch information...
1 parent a63566d commit f576d7cf848717384799a9e9669b253ccc94deb5 Neeraj Singh committed with tenderlove Jul 19, 2010
View
4 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -49,9 +49,9 @@ def insert_record(record, force = true, validate = true)
attributes = columns.inject({}) do |attrs, column|
case column.name.to_s
when @reflection.primary_key_name.to_s
- attrs[relation[column.name]] = owner_quoted_id
+ attrs[relation[column.name]] = @owner.send(:id)
when @reflection.association_foreign_key.to_s
- attrs[relation[column.name]] = record.quoted_id
+ attrs[relation[column.name]] = record.send(:id)
else
if record.has_attribute?(column.name)
value = @owner.send(:quote_value, record[column.name], column)
View
18 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -24,6 +24,8 @@
require 'models/member'
require 'models/membership'
require 'models/sponsor'
+require 'models/country'
+require 'models/treaty'
require 'active_support/core_ext/string/conversions'
class ProjectWithAfterCreateHook < ActiveRecord::Base
@@ -83,6 +85,22 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects,
:parrots, :pirates, :treasures, :price_estimates, :tags, :taggings
+ def test_should_property_quote_string_primary_keys
+ country = Country.new(:name => 'India')
+ country.country_id = 'c1'
+ country.save!
+
+ treaty = Treaty.new(:name => 'peace')
+ treaty.treaty_id = 't1'
+ country.treaties << treaty
+
+ con = ActiveRecord::Base.connection
+ sql = 'select * from countries_treaties'
+ record = con.select_rows(sql).last
+ assert_equal 'c1', record[0]
+ assert_equal 't1', record[1]
+ end
+
def test_has_and_belongs_to_many
david = Developer.find(1)
View
7 activerecord/test/models/country.rb
@@ -0,0 +1,7 @@
+class Country < ActiveRecord::Base
+
+ set_primary_key :country_id
+
+ has_and_belongs_to_many :treaties
+
+end
View
7 activerecord/test/models/treaty.rb
@@ -0,0 +1,7 @@
+class Treaty < ActiveRecord::Base
+
+ set_primary_key :treaty_id
+
+ has_and_belongs_to_many :countries
+
+end
View
13 activerecord/test/schema/schema.rb
@@ -600,6 +600,19 @@ def create_table(*args, &block)
t.string :title
end
+ create_table :countries, :force => true, :id => false, :primary_key => 'country_id' do |t|
+ t.string :country_id
+ t.string :name
+ end
+ create_table :treaties, :force => true, :id => false, :primary_key => 'treaty_id' do |t|
+ t.string :treaty_id
+ t.string :name
+ end
+ create_table :countries_treaties, :force => true, :id => false do |t|
+ t.string :country_id, :null => false
+ t.string :treaty_id, :null => false
+ end
+
except 'SQLite' do
# fk_test_has_fk should be before fk_test_has_pk
create_table :fk_test_has_fk, :force => true do |t|

0 comments on commit f576d7c

Please sign in to comment.
Something went wrong with that request. Please try again.