mirror of
https://github.com/github/rails.git
synced 2026-02-03 10:45:01 -05:00
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
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
2
activerecord/test/fixtures/post.rb
vendored
2
activerecord/test/fixtures/post.rb
vendored
@@ -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
|
||||
|
||||
|
||||
1
activerecord/test/fixtures/tag.rb
vendored
1
activerecord/test/fixtures/tag.rb
vendored
@@ -1,3 +1,4 @@
|
||||
class Tag < ActiveRecord::Base
|
||||
has_many :taggings, :as => :taggable
|
||||
has_one :tagging, :as => :taggable
|
||||
end
|
||||
Reference in New Issue
Block a user