mirror of
https://github.com/github/rails.git
synced 2026-01-09 14:48:01 -05:00
Fix polymorphic belongs_to associationproxy raising errors when loading target.
This commit is contained in:
committed by
Aaron Patterson
parent
a60ea74222
commit
7d4d745730
@@ -4,6 +4,11 @@ module ActiveRecord
|
||||
class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
|
||||
private
|
||||
|
||||
def conditions
|
||||
@conditions ||= interpolate_sql(target_klass.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]
|
||||
end
|
||||
alias :sql_conditions :conditions
|
||||
|
||||
def replace_keys(record)
|
||||
super
|
||||
@owner[@reflection.foreign_type] = record && record.class.base_class.name
|
||||
|
||||
@@ -146,6 +146,15 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||
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
|
||||
assert_equal Company.find(2, :include => :firm_with_select ).firm_with_select.attributes.size, 1
|
||||
|
||||
@@ -2,4 +2,6 @@ class Sponsor < ActiveRecord::Base
|
||||
belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id"
|
||||
belongs_to :sponsorable, :polymorphic => true
|
||||
belongs_to :thing, :polymorphic => true, :foreign_type => :sponsorable_type, :foreign_key => :sponsorable_id
|
||||
belongs_to :sponsorable_with_conditions, :polymorphic => true,
|
||||
:foreign_type => 'sponsorable_type', :foreign_key => 'sponsorable_id', :conditions => {:name => 'Ernie'}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user