diff options
Diffstat (limited to 'activerecord/lib/active_record')
9 files changed, 52 insertions, 62 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index b3d5a29b16..35fd1395f7 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1467,16 +1467,17 @@ module ActiveRecord force_reload = params.first unless params.empty? association = association_instance_get(reflection.name) - if association.nil? || force_reload || association.stale_target? + if association.nil? association = association_proxy_class.new(self, reflection) - retval = force_reload ? reflection.klass.uncached { association.reload } : association.reload - if retval.nil? and association_proxy_class == BelongsToAssociation - association_instance_set(reflection.name, nil) - return nil - end association_instance_set(reflection.name, association) end + if force_reload + reflection.klass.uncached { association.reload } + elsif !association.loaded? || association.stale_target? + association.reload + end + association.target.nil? ? nil : association end @@ -1485,19 +1486,19 @@ module ActiveRecord association && association.loaded? end - redefine_method("#{reflection.name}=") do |new_value| + redefine_method("#{reflection.name}=") do |record| association = association_instance_get(reflection.name) - if association.nil? || association.target != new_value + if association.nil? association = association_proxy_class.new(self, reflection) + association_instance_set(reflection.name, association) end - association.replace(new_value) - association_instance_set(reflection.name, new_value.nil? ? nil : association) + association.replace(record) + association.target.nil? ? nil : association end redefine_method("set_#{reflection.name}_target") do |target| - return if target.nil? and association_proxy_class == BelongsToAssociation association = association_proxy_class.new(self, reflection) association.target = target association_instance_set(reflection.name, association) diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 7e68241a2c..65fef81d64 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -128,17 +128,18 @@ module ActiveRecord # Asserts the \target has been loaded setting the \loaded flag to +true+. def loaded - @loaded = true + @loaded = true + @stale_state = stale_state end # The target is stale if the target no longer points to the record(s) that the # relevant foreign_key(s) refers to. If stale, the association accessor method - # on the owner will reload the target. It's up to subclasses to implement this - # method if relevant. + # on the owner will reload the target. It's up to subclasses to implement the + # state_state method if relevant. # # Note that if the target has not been loaded, it is not considered stale. def stale_target? - false + loaded? && @stale_state != stale_state end # Returns the target of this proxy, same as +proxy_target+. @@ -273,16 +274,19 @@ module ActiveRecord @target = find_target end - @loaded = true + loaded @target rescue ActiveRecord::RecordNotFound reset end - # Can be overwritten by associations that might have the foreign key - # available for an association without having the object itself (and - # still being a new record). Currently, only +belongs_to+ presents - # this scenario (both vanilla and polymorphic). + # Should be true if there is a foreign key present on the @owner which + # references the target. This is used to determine whether we can load + # the target if the @owner is currently a new record (and therefore + # without a key). + # + # Currently implemented by belongs_to (vanilla and polymorphic) and + # has_one/has_many :through associations which go through a belongs_to def foreign_key_present false end @@ -308,6 +312,15 @@ module ActiveRecord def invertible_for?(record) inverse_reflection_for(record) end + + # This should be implemented to return the values of the relevant key(s) on the owner, + # so that when state_state is different from the value stored on the last find_target, + # the target is stale. + # + # This is only relevant to certain associations, which is why it returns nil by default. + def stale_state + nil + end end end end diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 63eb38ab34..c54e397ae4 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -29,17 +29,6 @@ module ActiveRecord @updated end - def stale_target? - if @target && @target.persisted? - target_id = @target[@reflection.association_primary_key].to_s - foreign_key = @owner[@reflection.foreign_key].to_s - - target_id != foreign_key - else - false - end - end - private def update_counters(record) counter_cache_name = @reflection.counter_cache_column @@ -106,6 +95,10 @@ module ActiveRecord @owner[@reflection.foreign_key] end end + + def stale_state + @owner[@reflection.foreign_key].to_s + end end end end 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 4608ffad67..4f67b02d00 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -2,19 +2,6 @@ module ActiveRecord # = Active Record Belongs To Polymorphic Association module Associations class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc: - def stale_target? - if @target && @target.persisted? - target_id = @target.send(@reflection.association_primary_key).to_s - foreign_key = @owner.send(@reflection.foreign_key).to_s - target_type = @target.class.base_class.name - foreign_type = @owner.send(@reflection.foreign_type).to_s - - target_id != foreign_key || target_type != foreign_type - else - false - end - end - private def replace_keys(record) @@ -38,6 +25,10 @@ module ActiveRecord def raise_on_type_mismatch(record) # A polymorphic association cannot have a type mismatch, by definition end + + def stale_state + [super, @owner[@reflection.foreign_type].to_s] + end end end end diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 2348ee099c..d432e8486d 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -62,7 +62,6 @@ module ActiveRecord def find_target return [] unless target_reflection_has_associated_record? - update_stale_state scoped.all end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 12ccb3af8a..7bbeb8829a 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -33,10 +33,8 @@ module ActiveRecord case @reflection.options[:dependent] when :delete @target.delete if @target.persisted? - @owner.clear_association_cache when :destroy @target.destroy if @target.persisted? - @owner.clear_association_cache when :nullify @target[@reflection.foreign_key] = nil @target.save if @owner.persisted? && @target.persisted? @@ -56,7 +54,7 @@ module ActiveRecord end set_inverse_instance(obj) - @loaded = true + loaded unless !@owner.persisted? || obj.nil? || dont_save return (obj.save ? self : false) diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index c9ae930e93..11fa40a5c4 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -7,6 +7,7 @@ module ActiveRecord def replace(new_value) create_through_record(new_value) @target = new_value + loaded end private @@ -32,7 +33,6 @@ module ActiveRecord end def find_target - update_stale_state scoped.first end end diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index 6536fb44b2..0d83b46130 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -10,17 +10,6 @@ module ActiveRecord end end - def stale_target? - if @target && @reflection.through_reflection.macro == :belongs_to && defined?(@through_foreign_key) - previous_key = @through_foreign_key.to_s - current_key = @owner.send(@reflection.through_reflection.foreign_key).to_s - - previous_key != current_key - else - false - end - end - protected def construct_find_scope @@ -157,11 +146,16 @@ module ActiveRecord alias_method :sql_conditions, :conditions - def update_stale_state + def stale_state if @reflection.through_reflection.macro == :belongs_to - @through_foreign_key = @owner.send(@reflection.through_reflection.foreign_key) + @owner[@reflection.through_reflection.foreign_key].to_s end end + + def foreign_key_present + @reflection.through_reflection.macro == :belongs_to && + !@owner[@reflection.through_reflection.foreign_key].nil? + end end end end diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index c6c1dd8b87..70ed16eeaf 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -368,6 +368,7 @@ module ActiveRecord if association.updated? association_id = association.send(reflection.options[:primary_key] || :id) self[reflection.foreign_key] = association_id + association.loaded end saved if autosave |