Permalink
Browse files

Fix problem where wrong keys are used in JoinAssociation when an asso…

…ciation goes :through a belongs_to [#2801 state:resolved]
  • Loading branch information...
1 parent 9863d8a commit b8153fd5a18441567f787a33ca882acb3bb5088a @jonleighton jonleighton committed with tenderlove Dec 16, 2010
@@ -190,17 +190,20 @@ def join_has_many_through_to(relation)
).alias @aliased_join_table_name
jt_conditions = []
- jt_foreign_key = first_key = second_key = nil
+ first_key = second_key = nil
- if through_reflection.options[:as] # has_many :through against a polymorphic join
- as_key = through_reflection.options[:as].to_s
- jt_foreign_key = as_key + '_id'
-
- jt_conditions <<
- join_table[as_key + '_type'].
- eq(parent.active_record.base_class.name)
+ if through_reflection.macro == :belongs_to
+ jt_primary_key = through_reflection.primary_key_name
+ jt_foreign_key = through_reflection.association_primary_key
else
+ jt_primary_key = through_reflection.active_record_primary_key
jt_foreign_key = through_reflection.primary_key_name
+
+ if through_reflection.options[:as] # has_many :through against a polymorphic join
+ jt_conditions <<
+ join_table["#{through_reflection.options[:as]}_type"].
+ eq(parent.active_record.base_class.name)
+ end
end
case source_reflection.macro
@@ -233,7 +236,7 @@ def join_has_many_through_to(relation)
end
jt_conditions <<
- parent_table[parent.primary_key].
+ parent_table[jt_primary_key].
eq(join_table[jt_foreign_key])
if through_reflection.options[:conditions]
@@ -208,6 +208,10 @@ def association_foreign_key
@association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key
end
+ def association_primary_key
+ @association_primary_key ||= options[:primary_key] || klass.primary_key
+ end
+
def active_record_primary_key
@active_record_primary_key ||= options[:primary_key] || active_record.primary_key
end
@@ -479,4 +479,11 @@ def test_joining_has_many_through_with_uniq
assert_equal 1, mary.unique_categorized_posts.length
assert_equal 1, mary.unique_categorized_post_ids.length
end
+
+ def test_joining_has_many_through_belongs_to
+ posts = Post.joins(:author_categorizations).
+ where('categorizations.id' => categorizations(:mary_thinking_sti).id)
+
+ assert_equal [posts(:eager_other)], posts
+ end
end
@@ -191,6 +191,11 @@ def test_has_many_through_reflection
assert_kind_of ThroughReflection, Subscriber.reflect_on_association(:books)
end
+ def test_association_primary_key
+ assert_equal "id", Author.reflect_on_association(:posts).association_primary_key.to_s
+ assert_equal "name", Author.reflect_on_association(:essay).association_primary_key.to_s
+ end
+
def test_active_record_primary_key
assert_equal "nick", Subscriber.reflect_on_association(:subscriptions).active_record_primary_key.to_s
assert_equal "name", Author.reflect_on_association(:essay).active_record_primary_key.to_s
@@ -38,6 +38,7 @@ def find_most_recent
end
has_many :author_favorites, :through => :author
+ has_many :author_categorizations, :through => :author, :source => :categorizations
has_many :comments_with_interpolated_conditions, :class_name => 'Comment',
:conditions => ['#{"#{aliased_table_name}." rescue ""}body = ?', 'Thank you for the welcome']

0 comments on commit b8153fd

Please sign in to comment.