From cdfd013dd7953fc87038c73ecddbaa8cfe29a301 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Wed, 15 Sep 2010 12:26:54 -0300 Subject: Set attributes properly for model built from association with conditions [#5562 state:resolved] Signed-off-by: Santiago Pastorino --- activerecord/lib/active_record/base.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/base.rb') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a9361f96f0..2157a0aded 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1384,10 +1384,7 @@ MSG ensure_proper_type - if scope = self.class.send(:current_scoped_methods) - create_with = scope.scope_for_create - create_with.each { |att,value| self.send("#{att}=", value) } if create_with - end + populate_with_current_scope_attributes self.attributes = attributes unless attributes.nil? result = yield self if block_given? @@ -1416,10 +1413,7 @@ MSG @new_record = true ensure_proper_type - if scope = self.class.send(:current_scoped_methods) - create_with = scope.scope_for_create - create_with.each { |att,value| self.send("#{att}=", value) } if create_with - end + populate_with_current_scope_attributes end # Returns a String, which Action Pack uses for constructing an URL to this @@ -1808,6 +1802,13 @@ MSG return string unless string.is_a?(String) && string =~ /^---/ YAML::load(string) rescue string end + + def populate_with_current_scope_attributes + if scope = self.class.send(:current_scoped_methods) + create_with = scope.scope_for_create + create_with.each { |att,value| self.respond_to?(:"#{att}=") && self.send("#{att}=", value) } if create_with + end + end end Base.class_eval do -- cgit v1.2.3