Permalink
Browse files

This patch allows a user to join through a polymorphic has_one associ…

…ation to another has_one polymorphic association. This also includes a fix in to the Author model test to properly define two associations to taggings as a has_one and a has_many.
  • Loading branch information...
1 parent b538e85 commit 05253064b78c742e8721372b3b00da4c6c5d7ebf @daphonz daphonz committed Apr 30, 2011
@@ -2174,7 +2174,7 @@ def association_join
end
case source_reflection.macro
- when :has_many
+ when :has_many, :has_one
if source_reflection.options[:as]
first_key = "#{source_reflection.options[:as]}_id"
second_key = options[:foreign_key] || primary_key
@@ -7,7 +7,7 @@
require 'models/tagging'
class InnerJoinAssociationTest < ActiveRecord::TestCase
- fixtures :authors, :posts, :comments, :categories, :categories_posts, :categorizations
+ fixtures :authors, :posts, :comments, :categories, :categories_posts, :categorizations, :taggings, :tags
def test_construct_finder_sql_applies_aliases_tables_on_association_conditions
result = Author.joins(:thinking_posts, :welcome_posts).to_a
@@ -71,4 +71,10 @@ def test_calculate_honors_implicit_inner_joins_and_distinct_and_conditions
authors_with_welcoming_post_titles = Author.calculate(:count, 'authors.id', :joins => :posts, :distinct => true, :conditions => "posts.title like 'Welcome%'")
assert_equal real_count, authors_with_welcoming_post_titles, "inner join and conditions should have only returned authors posting titles starting with 'Welcome'"
end
+
+ def test_find_on_polymorphic_has_one_association
+ # Author => has_one :tagging, :through => :posts
+ # Post => has_one :tagging, :as => :taggable
+ assert_equal Author.joins(:posts => :tagging).all, Author.joins(:tagging).all
+ end
end
@@ -379,7 +379,7 @@ def test_has_many_through_has_many_find_by_id
end
def test_has_many_through_polymorphic_has_one
- assert_equal Tagging.find(1,2).sort_by { |t| t.id }, authors(:david).tagging
+ assert_equal Tagging.find(1,2).sort_by { |t| t.id }, authors(:david).taggings
end
def test_has_many_through_polymorphic_has_many
@@ -83,7 +83,7 @@ def testing_proxy_target
has_many :author_favorites
has_many :favorite_authors, :through => :author_favorites, :order => 'name'
- has_many :tagging, :through => :posts # through polymorphic has_one
+ has_one :tagging, :through => :posts # through polymorphic has_one
has_many :taggings, :through => :posts, :source => :taggings # through polymorphic has_many
has_many :tags, :through => :posts # through has_many :through
has_many :post_categories, :through => :posts, :source => :categories

0 comments on commit 0525306

Please sign in to comment.