diff options
author | Murray Steele <muz@h-lame.com> | 2009-12-17 12:19:10 +0000 |
---|---|---|
committer | Eloy Duran <eloy.de.enige@gmail.com> | 2009-12-28 15:12:15 +0100 |
commit | 6a74ee7f4deea4a44520d3fcc9120e0bb848823f (patch) | |
tree | 6654d00f15bb5fc2598c21ffe1f31b76f979eb5a /activerecord/lib | |
parent | 81ca0cf2b074f4b868a84c427ef155607a956119 (diff) | |
download | rails-6a74ee7f4deea4a44520d3fcc9120e0bb848823f.tar.gz rails-6a74ee7f4deea4a44520d3fcc9120e0bb848823f.tar.bz2 rails-6a74ee7f4deea4a44520d3fcc9120e0bb848823f.zip |
Provide a slightly more robust we_can_set_the_inverse_on_this? method for polymorphic belongs_to associations. [#3520 state:resolved]
Also add a new test for polymorphic belongs_to that test direct accessor assignment, not just .replace assignment.
Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb | 9 |
1 files changed, 7 insertions, 2 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 9678300003..f6edd6383c 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -27,7 +27,12 @@ module ActiveRecord # NOTE - for now, we're only supporting inverse setting from belongs_to back onto # has_one associations. def we_can_set_the_inverse_on_this?(record) - @reflection.has_inverse? && @reflection.polymorphic_inverse_of(record.class).macro == :has_one + if @reflection.has_inverse? + inverse_association = @reflection.polymorphic_inverse_of(record.class) + inverse_association && inverse_association.macro == :has_one + else + false + end end def set_inverse_instance(record, instance) @@ -52,7 +57,7 @@ module ActiveRecord else association_class.find(@owner[@reflection.primary_key_name], :select => @reflection.options[:select], :include => @reflection.options[:include]) end - set_inverse_instance(target, @owner) if target + set_inverse_instance(target, @owner) target end |