mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
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]
This commit is contained in:
committed by
Michael Koziarski
parent
646b5bfa61
commit
fcf31cb752
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user