mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure has_many :through works with changed primary keys [#736 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
1ab2ff58ed
commit
1e6c50e21b
7 changed files with 26 additions and 4 deletions
|
@ -150,7 +150,7 @@ module ActiveRecord
|
|||
end
|
||||
else
|
||||
reflection_primary_key = @reflection.source_reflection.primary_key_name
|
||||
source_primary_key = @reflection.klass.primary_key
|
||||
source_primary_key = @reflection.through_reflection.klass.primary_key
|
||||
if @reflection.source_reflection.options[:as]
|
||||
polymorphic_join = "AND %s.%s = %s" % [
|
||||
@reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type",
|
||||
|
|
|
@ -8,9 +8,12 @@ require 'models/comment'
|
|||
require 'models/tag'
|
||||
require 'models/tagging'
|
||||
require 'models/author'
|
||||
require 'models/owner'
|
||||
require 'models/pet'
|
||||
require 'models/toy'
|
||||
|
||||
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||
fixtures :posts, :readers, :people, :comments, :authors
|
||||
fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys
|
||||
|
||||
def test_associate_existing
|
||||
assert_queries(2) { posts(:thinking);people(:david) }
|
||||
|
@ -251,4 +254,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
|||
author.author_favorites.create(:favorite_author_id => 3)
|
||||
assert_equal post.author.author_favorites, post.author_favorites
|
||||
end
|
||||
|
||||
def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys
|
||||
assert_equal 1, owners(:blackbeard).toys.count
|
||||
end
|
||||
end
|
||||
|
|
4
activerecord/test/fixtures/toys.yml
vendored
Normal file
4
activerecord/test/fixtures/toys.yml
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
bone:
|
||||
toy_id: 1
|
||||
name: Bone
|
||||
pet_id: 1
|
|
@ -1,4 +1,5 @@
|
|||
class Owner < ActiveRecord::Base
|
||||
set_primary_key :owner_id
|
||||
has_many :pets
|
||||
end
|
||||
has_many :toys, :through => :pets
|
||||
end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
class Pet < ActiveRecord::Base
|
||||
set_primary_key :pet_id
|
||||
belongs_to :owner
|
||||
end
|
||||
has_many :toys
|
||||
end
|
||||
|
|
4
activerecord/test/models/toy.rb
Normal file
4
activerecord/test/models/toy.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class Toy < ActiveRecord::Base
|
||||
set_primary_key :toy_id
|
||||
belongs_to :pet
|
||||
end
|
|
@ -425,6 +425,11 @@ ActiveRecord::Schema.define do
|
|||
t.column :taggings_count, :integer, :default => 0
|
||||
end
|
||||
|
||||
create_table :toys, :primary_key => :toy_id ,:force => true do |t|
|
||||
t.string :name
|
||||
t.integer :pet_id, :integer
|
||||
end
|
||||
|
||||
create_table :treasures, :force => true do |t|
|
||||
t.column :name, :string
|
||||
t.column :looter_id, :integer
|
||||
|
|
Loading…
Reference in a new issue