提交 915ea5ea 编写于 作者: J Jon Leighton

Support the :primary_key option on a through reflection in a nested through association

上级 0ceb3429
......@@ -76,14 +76,11 @@ def construct_through_joins
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 @@ def construct_through_joins
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/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 @@ def test_nested_has_many_through_with_conditions_on_source_associations
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)
......
......@@ -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 @@ def testing_proxy_target
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
\ No newline at end of file
end
......@@ -44,6 +44,8 @@ def create_table(*args, &block)
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|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册