aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2008-09-07 14:42:33 +0100
committerMichael Koziarski <michael@koziarski.com>2008-09-13 11:08:29 +0200
commitfcf31cb7521ba7de0aae972ac2ddfc80e3e7dfce (patch)
treeb01347616c463b6c51f8761e0d9d400c1e775300 /activerecord/lib
parent646b5bfa61f8e803d98bd1557c53ea961cc6b16d (diff)
downloadrails-fcf31cb7521ba7de0aae972ac2ddfc80e3e7dfce.tar.gz
rails-fcf31cb7521ba7de0aae972ac2ddfc80e3e7dfce.tar.bz2
rails-fcf31cb7521ba7de0aae972ac2ddfc80e3e7dfce.zip
Support for updating a belongs to association from the foreign key (without saving and reloading the record)
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#142 state:committed]
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb9
1 files changed, 8 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index aca2d770fc..5d91315aad 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1237,7 +1237,7 @@ module ActiveRecord
association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
- if association.nil? || force_reload
+ if association.nil? || !association.loaded? || force_reload
association = association_proxy_class.new(self, reflection)
retval = association.reload
if retval.nil? and association_proxy_class == BelongsToAssociation
@@ -1266,6 +1266,13 @@ module ActiveRecord
end
end
+ if association_proxy_class == BelongsToAssociation
+ define_method("#{reflection.primary_key_name}=") do |target_id|
+ instance_variable_get(ivar).reset if instance_variable_defined?(ivar)
+ write_attribute(reflection.primary_key_name, target_id)
+ end
+ end
+
define_method("set_#{reflection.name}_target") do |target|
return if target.nil? and association_proxy_class == BelongsToAssociation
association = association_proxy_class.new(self, reflection)