提交 b3065a51 编写于 作者: J Jeremy Kemper

Polymorphic join support for has_one associations (has_one :foo, :as => :bar). Closes #3785.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3558 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 c326851e
*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]
......
......@@ -899,7 +899,7 @@ def create_has_many_reflection(association_id, options, &extension)
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)
......
......@@ -66,9 +66,15 @@ def find_target
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
......
......@@ -26,6 +26,10 @@ def test_inherited_has_many
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 @@ def test_polymorphic_has_many_create_model_with_inheritance
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
......
......@@ -23,6 +23,8 @@ def find_most_recent
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
......
class Tag < ActiveRecord::Base
has_many :taggings, :as => :taggable
has_one :tagging, :as => :taggable
end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册