From 50aafa3c1f1c04bb84a37532c938ce6d7c5e09c5 Mon Sep 17 00:00:00 2001 From: dira Date: Sun, 9 Aug 2009 18:36:45 +0300 Subject: [PATCH] tests for bug #2990 - belongs_to with primary_key in a has_many :through generates bad sql - primary key not taken into account --- .../has_many_through_associations_test.rb | 10 ++++++++++ activerecord/test/models/appointment.rb | 4 ++++ activerecord/test/models/authorship.rb | 1 + activerecord/test/models/patient.rb | 4 ++++ activerecord/test/models/person.rb | 1 + activerecord/test/models/physician.rb | 4 ++++ activerecord/test/schema/schema.rb | 11 +++++++++++ 7 files changed, 35 insertions(+), 0 deletions(-) create mode 100644 activerecord/test/models/appointment.rb create mode 100644 activerecord/test/models/authorship.rb create mode 100644 activerecord/test/models/patient.rb create mode 100644 activerecord/test/models/physician.rb diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 97efca7..78069ab 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -11,6 +11,9 @@ require 'models/author' require 'models/owner' require 'models/pet' require 'models/toy' +require 'models/patient' +require 'models/physician' +require 'models/appointment' class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys @@ -276,4 +279,11 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys assert_equal 1, owners(:blackbeard).toys.count end + + def test_has_many_association_through_nonstandard_primary_key_in_belongs_to + physician = Physician.create + patient = Patient.create + Appointment.create(:physician_id => physician.id, :custom_patient_id => patient.id) + Physician.first.patients.all + end end diff --git a/activerecord/test/models/appointment.rb b/activerecord/test/models/appointment.rb new file mode 100644 index 0000000..db66d51 --- /dev/null +++ b/activerecord/test/models/appointment.rb @@ -0,0 +1,4 @@ +class Appointment < ActiveRecord::Base + belongs_to :physician + belongs_to :patient, :primary_key => :custom_patient_id +end diff --git a/activerecord/test/models/authorship.rb b/activerecord/test/models/authorship.rb new file mode 100644 index 0000000..b5b9e8f --- /dev/null +++ b/activerecord/test/models/authorship.rb @@ -0,0 +1 @@ +class Authorship < ActiveRecord::Base diff --git a/activerecord/test/models/patient.rb b/activerecord/test/models/patient.rb new file mode 100644 index 0000000..72aae2f --- /dev/null +++ b/activerecord/test/models/patient.rb @@ -0,0 +1,4 @@ +class Patient < ActiveRecord::Base + has_many :appointments + has_many :physicians, :through => :appointments +end diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index ec2f684..ae8c14f 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -2,6 +2,7 @@ class Person < ActiveRecord::Base has_many :readers has_many :posts, :through => :readers has_many :posts_with_no_comments, :through => :readers, :source => :post, :include => :comments, :conditions => 'comments.id is null' + has_many :posts_by_title, :through => :authorship , :source => :post has_many :references has_many :jobs, :through => :references diff --git a/activerecord/test/models/physician.rb b/activerecord/test/models/physician.rb new file mode 100644 index 0000000..504fcda --- /dev/null +++ b/activerecord/test/models/physician.rb @@ -0,0 +1,4 @@ +class Physician < ActiveRecord::Base + has_many :appointments + has_many :patients, :through => :appointments +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index d080140..655fd64 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -26,6 +26,11 @@ ActiveRecord::Schema.define do t.integer :credit_limit end + create_table :appointments, :force => true do |t| + t.integer :custom_patient_id + t.integer :physician_id + end + create_table :audit_logs, :force => true do |t| t.column :message, :string, :null=>false t.column :developer_id, :integer, :null=>false @@ -326,6 +331,9 @@ ActiveRecord::Schema.define do t.column :treasure_id, :integer end + create_table :patients, :force => true do |t| + end + create_table :people, :force => true do |t| t.string :first_name, :null => false t.references :primary_contact @@ -338,6 +346,9 @@ ActiveRecord::Schema.define do t.integer :owner_id, :integer end + create_table :physicians, :force => true do |t| + end + create_table :pirates, :force => true do |t| t.column :catchphrase, :string t.column :parrot_id, :integer -- 1.6.3.3