allow has_many :through to work with custom :foreign key (closes #3422) [Rick Olson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3456 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2006-01-21 23:40:20 +00:00
parent 13cd6135cd
commit 1facce6c24
3 changed files with 9 additions and 3 deletions

View File

@@ -62,7 +62,6 @@ module ActiveRecord
def construct_conditions
through_reflection = @owner.class.reflections[@reflection.options[:through]]
through_foreign_classname = ActiveRecord::Base.send(:class_name_of_active_record_descendant, @owner.class).to_s
if through_reflection.options[:as]
conditions =
@@ -72,7 +71,7 @@ module ActiveRecord
else
conditions =
"#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{@reflection.klass.to_s.foreign_key} " +
"AND #{through_reflection.table_name}.#{through_foreign_classname.foreign_key} = #{@owner.quoted_id}"
"AND #{through_reflection.table_name}.#{through_reflection.primary_key_name} = #{@owner.quoted_id}"
end
conditions << " AND (#{interpolate_sql(sanitize_sql(@reflection.options[:conditions]))})" if @reflection.options[:conditions]

View File

@@ -68,5 +68,9 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
assert_equal categories(:general), authors(:david).categories.find_by_name('General')
# assert_equal nil, authors(:david).categories.find_by_name('Technology')
end
def test_has_many_going_through_join_model_with_custom_foreign_key
assert_equal [], posts(:thinking).authors
assert_equal [authors(:mary)], posts(:authorless).authors
end
end

View File

@@ -23,6 +23,9 @@ class Post < ActiveRecord::Base
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings
has_many :categorizations, :foreign_key => :category_id
has_many :authors, :through => :categorizations
def self.what_are_you
'a post...'
end