Created
January 7, 2011 01:10
-
-
Save ernie/768941 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 61ad8e150d74f6c2bafeaac73f1b40d11d3092c1 Mon Sep 17 00:00:00 2001 | |
From: Ernie Miller <[email protected]> | |
Date: Thu, 6 Jan 2011 17:13:01 -0500 | |
Subject: [PATCH] Fix polymorphic belongs_to associationproxy raising errors when loading target. | |
--- | |
.../belongs_to_polymorphic_association.rb | 4 ++++ | |
.../associations/belongs_to_associations_test.rb | 9 +++++++++ | |
activerecord/test/models/sponsor.rb | 2 ++ | |
3 files changed, 15 insertions(+), 0 deletions(-) | |
diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb | |
index 38454ec..076fc9d 100644 | |
--- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb | |
+++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb | |
@@ -23,6 +23,10 @@ module ActiveRecord | |
@updated | |
end | |
+ def conditions | |
+ @conditions ||= interpolate_sql(association_class.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions] | |
+ end | |
+ | |
private | |
# NOTE - for now, we're only supporting inverse setting from belongs_to back onto | |
diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb | |
index acc3b9e..88de463 100644 | |
--- a/activerecord/test/cases/associations/belongs_to_associations_test.rb | |
+++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb | |
@@ -151,6 +151,15 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase | |
assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name | |
assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm" | |
end | |
+ | |
+ def test_with_polymorphic_and_condition | |
+ sponsor = Sponsor.create | |
+ member = Member.create :name => "Bert" | |
+ sponsor.sponsorable = member | |
+ | |
+ assert_equal member, sponsor.sponsorable | |
+ assert_nil sponsor.sponsorable_with_conditions | |
+ end | |
def test_with_select | |
assert_equal Company.find(2).firm_with_select.attributes.size, 1 | |
diff --git a/activerecord/test/models/sponsor.rb b/activerecord/test/models/sponsor.rb | |
index 50c2c2d..25f983e 100644 | |
--- a/activerecord/test/models/sponsor.rb | |
+++ b/activerecord/test/models/sponsor.rb | |
@@ -1,4 +1,6 @@ | |
class Sponsor < ActiveRecord::Base | |
belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id" | |
belongs_to :sponsorable, :polymorphic => true | |
+ belongs_to :sponsorable_with_conditions, :polymorphic => true, | |
+ :foreign_type => 'sponsorable_type', :foreign_key => 'sponsorable_id', :conditions => {:name => 'Ernie'} | |
end | |
\ No newline at end of file | |
-- | |
1.7.2.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment