aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb2
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb10
-rw-r--r--activerecord/test/associations_join_model_test.rb11
-rw-r--r--activerecord/test/fixtures/post.rb2
-rw-r--r--activerecord/test/fixtures/tag.rb1
6 files changed, 24 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 1a287ec7d8..c3bae2c841 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Polymorphic join support for has_one associations (has_one :foo, :as => :bar) #3785 [Rick Olson]
+
* PostgreSQL: correctly parse negative integer column defaults. #3776 [bellis@deepthought.org]
* Fix problems with count when used with :include [Jeremy Hopple and Kevin Clark]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 6b461b5514..0dc03487cb 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -899,7 +899,7 @@ module ActiveRecord
def create_has_one_reflection(association_id, options)
options.assert_valid_keys(
- :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend
+ :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as
)
reflection = create_reflection(:has_one, association_id, options, self)
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 17483305fc..4dbc0f6441 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -66,9 +66,15 @@ module ActiveRecord
end
def construct_sql
- @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}"
+ case
+ when @reflection.options[:as]
+ @finder_sql =
+ "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +
+ "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, @owner.class).to_s}'"
+ else
+ @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}"
+ end
@finder_sql << " AND (#{sanitize_sql(@reflection.options[:conditions])})" if @reflection.options[:conditions]
- @finder_sql
end
end
end
diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb
index 61488900d6..5437e049a3 100644
--- a/activerecord/test/associations_join_model_test.rb
+++ b/activerecord/test/associations_join_model_test.rb
@@ -26,6 +26,10 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
def test_polymorphic_has_many
assert_equal taggings(:welcome_general), posts(:welcome).taggings.first
end
+
+ def test_polymorphic_has_one
+ assert_equal taggings(:welcome_general), posts(:welcome).tagging
+ end
def test_polymorphic_belongs_to
assert_equal posts(:welcome), posts(:welcome).taggings.first.taggable
@@ -46,7 +50,12 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
tagging = tags(:misc).taggings.create(:taggable => post)
assert_equal "Post", tagging.taggable_type
end
-
+
+ def test_polymorphic_has_one_create_model_with_inheritance
+ tagging = tags(:misc).create_tagging(:taggable => posts(:thinking))
+ assert_equal "Post", tagging.taggable_type
+ end
+
def test_has_many_with_piggyback
assert_equal "2", categories(:sti_test).authors.first.post_id.to_s
end
diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb
index ada18af115..4c2ccb3f43 100644
--- a/activerecord/test/fixtures/post.rb
+++ b/activerecord/test/fixtures/post.rb
@@ -23,6 +23,8 @@ class Post < ActiveRecord::Base
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings
+ has_one :tagging, :as => :taggable
+
has_many :categorizations, :foreign_key => :category_id
has_many :authors, :through => :categorizations
diff --git a/activerecord/test/fixtures/tag.rb b/activerecord/test/fixtures/tag.rb
index d54f3cd680..78b6f6666c 100644
--- a/activerecord/test/fixtures/tag.rb
+++ b/activerecord/test/fixtures/tag.rb
@@ -1,3 +1,4 @@
class Tag < ActiveRecord::Base
has_many :taggings, :as => :taggable
+ has_one :tagging, :as => :taggable
end \ No newline at end of file