mirror of
https://github.com/github/rails.git
synced 2026-01-09 14:48:01 -05:00
Support the :primary_key option on a through reflection in a nested through association
This commit is contained in:
@@ -76,14 +76,11 @@ module ActiveRecord
|
||||
right_table_and_alias = table_name_and_alias(right.quoted_table_name, table_aliases[right])
|
||||
|
||||
if left.source_reflection.nil?
|
||||
# TODO: Perhaps need to pay attention to left.options[:primary_key] and
|
||||
# left.options[:foreign_key] in places here
|
||||
|
||||
case left.macro
|
||||
when :belongs_to
|
||||
joins << inner_join_sql(
|
||||
right_table_and_alias,
|
||||
table_aliases[left], left.klass.primary_key,
|
||||
table_aliases[left], left.association_primary_key,
|
||||
table_aliases[right], left.primary_key_name,
|
||||
reflection_conditions(right_index)
|
||||
)
|
||||
@@ -91,7 +88,7 @@ module ActiveRecord
|
||||
joins << inner_join_sql(
|
||||
right_table_and_alias,
|
||||
table_aliases[left], left.primary_key_name,
|
||||
table_aliases[right], right.klass.primary_key,
|
||||
table_aliases[right], right.association_primary_key,
|
||||
polymorphic_conditions(left, left),
|
||||
reflection_conditions(right_index)
|
||||
)
|
||||
|
||||
@@ -21,6 +21,7 @@ require 'models/organization'
|
||||
require 'models/category'
|
||||
require 'models/categorization'
|
||||
require 'models/membership'
|
||||
require 'models/essay'
|
||||
|
||||
# NOTE: Some of these tests might not really test "nested" HMT associations, as opposed to ones which
|
||||
# are just one level deep. But it's all the same thing really, as the "nested" code is being
|
||||
@@ -31,7 +32,7 @@ class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||
fixtures :authors, :books, :posts, :subscriptions, :subscribers, :tags, :taggings,
|
||||
:people, :readers, :references, :jobs, :ratings, :comments, :members, :member_details,
|
||||
:member_types, :sponsors, :clubs, :organizations, :categories, :categories_posts,
|
||||
:categorizations, :memberships
|
||||
:categorizations, :memberships, :essays
|
||||
|
||||
# Through associations can either use the has_many or has_one macros.
|
||||
#
|
||||
@@ -440,6 +441,20 @@ class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
def test_nested_has_many_through_with_foreign_key_option_on_the_source_reflection_through_reflection
|
||||
assert_equal [categories(:general)], organizations(:nsa).author_essay_categories
|
||||
|
||||
organizations = Organization.joins(:author_essay_categories).
|
||||
where('categories.id' => categories(:general).id)
|
||||
assert_equal [organizations(:nsa)], organizations
|
||||
|
||||
assert_equal categories(:general), organizations(:nsa).author_owned_essay_category
|
||||
|
||||
organizations = Organization.joins(:author_owned_essay_category).
|
||||
where('categories.id' => categories(:general).id)
|
||||
assert_equal [organizations(:nsa)], organizations
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assert_includes_and_joins_equal(query, expected, association)
|
||||
|
||||
2
activerecord/test/fixtures/authors.yml
vendored
2
activerecord/test/fixtures/authors.yml
vendored
@@ -3,6 +3,8 @@ david:
|
||||
name: David
|
||||
author_address_id: 1
|
||||
author_address_extra_id: 2
|
||||
organization_id: No Such Agency
|
||||
owned_essay_id: A Modest Proposal
|
||||
|
||||
mary:
|
||||
id: 2
|
||||
|
||||
@@ -110,6 +110,9 @@ class Author < ActiveRecord::Base
|
||||
has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
|
||||
has_many :essay_categories_2, :through => :essays_2, :source => :category
|
||||
|
||||
belongs_to :owned_essay, :primary_key => :name, :class_name => 'Essay'
|
||||
has_one :owned_essay_category, :through => :owned_essay, :source => :category
|
||||
|
||||
belongs_to :author_address, :dependent => :destroy
|
||||
belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
|
||||
|
||||
|
||||
@@ -2,5 +2,11 @@ class Organization < ActiveRecord::Base
|
||||
has_many :member_details
|
||||
has_many :members, :through => :member_details
|
||||
|
||||
has_many :authors, :primary_key => :name
|
||||
has_many :author_essay_categories, :through => :authors, :source => :essay_categories
|
||||
|
||||
has_one :author, :primary_key => :name
|
||||
has_one :author_owned_essay_category, :through => :author, :source => :owned_essay_category
|
||||
|
||||
scope :clubs, { :from => 'clubs' }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,6 +44,8 @@ ActiveRecord::Schema.define do
|
||||
t.string :name, :null => false
|
||||
t.integer :author_address_id
|
||||
t.integer :author_address_extra_id
|
||||
t.string :organization_id
|
||||
t.string :owned_essay_id
|
||||
end
|
||||
|
||||
create_table :author_addresses, :force => true do |t|
|
||||
|
||||
Reference in New Issue
Block a user