Skip to content
This repository
Browse code

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...
commit 93c557828e1873004911acfd25d3b3903210bc40 1 parent 628b4ad
Ruy Asan authored May 01, 2009 jeremy committed May 01, 2009
2  activerecord/lib/active_record/associations/has_one_association.rb
@@ -88,7 +88,7 @@ def construct_sql
88 88
             when @reflection.options[:as]
89 89
               @finder_sql = 
90 90
                 "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{owner_quoted_id} AND " +
91  
-                "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
  91
+                "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.name.to_s)}"
92 92
             else
93 93
               @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}"
94 94
           end
9  activerecord/test/cases/associations/has_one_associations_test.rb
@@ -2,9 +2,11 @@
2 2
 require 'models/developer'
3 3
 require 'models/project'
4 4
 require 'models/company'
  5
+require 'models/sponsor'
  6
+require 'models/organization'
5 7
 
6 8
 class HasOneAssociationsTest < ActiveRecord::TestCase
7  
-  fixtures :accounts, :companies, :developers, :projects, :developers_projects
  9
+  fixtures :accounts, :companies, :developers, :projects, :developers_projects, :organizations, :sponsors
8 10
 
9 11
   def setup
10 12
     Account.destroyed_account_ids.clear
@@ -306,4 +308,9 @@ def test_save_of_record_with_loaded_has_one
306 308
       Firm.find(@firm.id, :include => :account).save!
307 309
     end
308 310
   end
  311
+
  312
+  def test_polymorphic_sti
  313
+    assert_equal organizations(:sponsorable), sponsors(:org_sponsor).sponsorable
  314
+    assert_equal sponsors(:org_sponsor),      organizations(:sponsorable).sponsor
  315
+  end
309 316
 end
4  activerecord/test/fixtures/organizations.yml
@@ -2,4 +2,6 @@ nsa:
2 2
   name: No Such Agency
3 3
 discordians:
4 4
   name: Discordians
5  
-
  5
+sponsorable:
  6
+  name: We Need Money
  7
+  type: SponsorableOrganization
4  activerecord/test/fixtures/sponsors.yml
@@ -6,4 +6,6 @@ boring_club_sponsor_for_groucho:
6 6
   sponsorable: some_other_guy (Member)
7 7
 crazy_club_sponsor_for_groucho:
8 8
   sponsor_club: crazy_club
9  
-  sponsorable: some_other_guy (Member)
  9
+  sponsorable: some_other_guy (Member)
  10
+org_sponsor:
  11
+  sponsorable: sponsorable (SponsorableOrganization)
4  activerecord/test/models/organization.rb
... ...
@@ -1,4 +1,8 @@
1 1
 class Organization < ActiveRecord::Base
2 2
   has_many :member_details
3 3
   has_many :members, :through => :member_details
  4
+end
  5
+
  6
+class SponsorableOrganization < Organization
  7
+  has_one  :sponsor, :as => :sponsorable
4 8
 end
3  activerecord/test/schema/schema.rb
@@ -283,6 +283,7 @@ def create_table(*args, &block)
283 283
 
284 284
   create_table :organizations, :force => true do |t|
285 285
     t.string :name
  286
+    t.string :type
286 287
   end
287 288
 
288 289
   create_table :owners, :primary_key => :owner_id ,:force => true do |t|
@@ -388,7 +389,7 @@ def create_table(*args, &block)
388 389
   create_table :sponsors, :force => true do |t|
389 390
     t.integer :club_id
390 391
     t.integer :sponsorable_id
391  
-    t.string :sponsorable_type
  392
+    t.string  :sponsorable_type
392 393
   end
393 394
 
394 395
   create_table :subscribers, :force => true, :id => false do |t|

0 notes on commit 93c5578

Please sign in to comment.
Something went wrong with that request. Please try again.