Fix polymorphic has_one associations declared in an abstract class. Closes #8638.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7119 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2007-06-25 22:42:19 +00:00
parent 3aadfcef88
commit 89c79b8b95
7 changed files with 37 additions and 12 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Fix polymorphic has_one associations declared in an abstract class. #8638 [lifofifo, daxhuiberts]
* Fixed validates_associated should not stop on the first error #4276 [mrj/manfred/josh]
* Rollback if commit raises an exception. #8642 [kik, Jeremy Kemper]

View File

@ -1681,7 +1681,7 @@ module ActiveRecord
as_extra
]
when reflection.macro == :has_many && reflection.options[:as]
when reflection.options[:as] && [:has_many, :has_one].include?(reflection.macro)
" LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s" % [
table_name_and_alias,
aliased_table_name, "#{reflection.options[:as]}_id",
@ -1689,14 +1689,6 @@ module ActiveRecord
aliased_table_name, "#{reflection.options[:as]}_type",
klass.quote_value(parent.active_record.base_class.name)
]
when reflection.macro == :has_one && reflection.options[:as]
" LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [
table_name_and_alias,
aliased_table_name, "#{reflection.options[:as]}_id",
parent.aliased_table_name, parent.primary_key,
aliased_table_name, "#{reflection.options[:as]}_type",
klass.quote_value(reflection.active_record.base_class.name)
]
else
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
" LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [

View File

@ -2,6 +2,7 @@ require 'abstract_unit'
require 'fixtures/tag'
require 'fixtures/tagging'
require 'fixtures/post'
require 'fixtures/item'
require 'fixtures/comment'
require 'fixtures/author'
require 'fixtures/category'
@ -11,7 +12,7 @@ require 'fixtures/edge'
class AssociationsJoinModelTest < Test::Unit::TestCase
self.use_transactional_fixtures = false
fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices
fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices, :items
def test_has_many
assert authors(:david).categories.include?(categories(:general))
@ -229,7 +230,15 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
assert_equal tagging, post.tagging
end
end
def test_include_polymorphic_has_one_defined_in_abstract_parent
item = Item.find_by_id(items(:dvd).id, :include => :tagging)
tagging = taggings(:godfather)
assert_no_queries do
assert_equal tagging, item.tagging
end
end
def test_include_polymorphic_has_many_through
posts = Post.find(:all, :order => 'posts.id')
posts_with_tags = Post.find(:all, :include => :tags, :order => 'posts.id')

View File

@ -57,6 +57,10 @@ ActiveRecord::Schema.define do
create_table :lock_without_defaults_cust, :force => true do |t|
t.column :custom_lock_version, :integer
end
create_table :items, :force => true do |t|
t.column :name, :integer
end
# For sqlite 3.1.0+, make a table with a autoincrement column
if adapter_name == 'SQLite' and supports_autoincrement?

7
activerecord/test/fixtures/item.rb vendored Normal file
View File

@ -0,0 +1,7 @@
class AbstractItem < ActiveRecord::Base
self.abstract_class = true
has_one :tagging, :as => :taggable
end
class Item < AbstractItem
end

4
activerecord/test/fixtures/items.yml vendored Normal file
View File

@ -0,0 +1,4 @@
dvd:
id: 1
name: Godfather

View File

@ -15,4 +15,11 @@ fake:
id: 3
tag_id: 1
taggable_id: 1
taggable_type: FakeModel
taggable_type: FakeModel
godfather:
id: 4
tag_id: 1
taggable_id: 1
taggable_type: Item