Browse files

Fixed bug with polymorphic has_one :as pointing to an STI record

[#2594 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent db0bfe4 commit 99c103be1165da9c8299bc0977188ecf167e06a5 @rubyruy rubyruy committed with jeremy May 1, 2009
View
2 activerecord/lib/active_record/associations/has_one_association.rb
@@ -88,7 +88,7 @@ def construct_sql
when @reflection.options[:as]
@finder_sql =
"#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{owner_quoted_id} AND " +
- "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
+ "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.name.to_s)}"
else
@finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}"
end
View
9 activerecord/test/cases/associations/has_one_associations_test.rb
@@ -2,9 +2,11 @@
require 'models/developer'
require 'models/project'
require 'models/company'
+require 'models/sponsor'
+require 'models/organization'
class HasOneAssociationsTest < ActiveRecord::TestCase
- fixtures :accounts, :companies, :developers, :projects, :developers_projects
+ fixtures :accounts, :companies, :developers, :projects, :developers_projects, :organizations, :sponsors
def setup
Account.destroyed_account_ids.clear
@@ -306,4 +308,9 @@ def test_save_of_record_with_loaded_has_one
Firm.find(@firm.id, :include => :account).save!
end
end
+
+ def test_polymorphic_sti
+ assert_equal organizations(:sponsorable), sponsors(:org_sponsor).sponsorable
+ assert_equal sponsors(:org_sponsor), organizations(:sponsorable).sponsor
+ end
end
View
4 activerecord/test/fixtures/organizations.yml
@@ -2,4 +2,6 @@ nsa:
name: No Such Agency
discordians:
name: Discordians
-
+sponsorable:
+ name: We Need Money
+ type: SponsorableOrganization
View
4 activerecord/test/fixtures/sponsors.yml
@@ -6,4 +6,6 @@ boring_club_sponsor_for_groucho:
sponsorable: some_other_guy (Member)
crazy_club_sponsor_for_groucho:
sponsor_club: crazy_club
- sponsorable: some_other_guy (Member)
+ sponsorable: some_other_guy (Member)
+org_sponsor:
+ sponsorable: sponsorable (SponsorableOrganization)
View
4 activerecord/test/models/organization.rb
@@ -1,4 +1,8 @@
class Organization < ActiveRecord::Base
has_many :member_details
has_many :members, :through => :member_details
+end
+
+class SponsorableOrganization < Organization
+ has_one :sponsor, :as => :sponsorable
end
View
3 activerecord/test/schema/schema.rb
@@ -283,6 +283,7 @@ def create_table(*args, &block)
create_table :organizations, :force => true do |t|
t.string :name
+ t.string :type
end
create_table :owners, :primary_key => :owner_id ,:force => true do |t|
@@ -388,7 +389,7 @@ def create_table(*args, &block)
create_table :sponsors, :force => true do |t|
t.integer :club_id
t.integer :sponsorable_id
- t.string :sponsorable_type
+ t.string :sponsorable_type
end
create_table :subscribers, :force => true, :id => false do |t|

1 comment on commit 99c103b

Please sign in to comment.