diff options
Diffstat (limited to 'activerecord/lib')
3 files changed, 11 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 443ccaaa72..671c4c56df 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -214,7 +214,7 @@ module ActiveRecord target.size elsif !association_scope.group_values.empty? load_target.size - elsif !association_scope.distinct_value && target.is_a?(Array) + elsif !association_scope.distinct_value && !target.empty? unsaved_records = target.select(&:new_record?) unsaved_records.size + count_records else @@ -234,7 +234,7 @@ module ActiveRecord if loaded? size.zero? else - @target.blank? && !scope.exists? + target.empty? && !scope.exists? end end 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 491282adf7..019bf0729f 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -28,7 +28,11 @@ module ActiveRecord end if through_record - through_record.update(attributes) + if through_record.new_record? + through_record.assign_attributes(attributes) + else + through_record.update(attributes) + end elsif owner.new_record? || !save through_proxy.build(attributes) else diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index c29f7e7115..c055b97061 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -378,19 +378,19 @@ module ActiveRecord # === Examples # # # Touch all records - # Person.all.touch + # Person.all.touch_all # # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670'" # # # Touch multiple records with a custom attribute - # Person.all.touch(:created_at) + # Person.all.touch_all(:created_at) # # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670', \"created_at\" = '2018-01-04 22:55:23.132670'" # # # Touch multiple records with a specified time - # Person.all.touch(time: Time.new(2020, 5, 16, 0, 0, 0)) + # Person.all.touch_all(time: Time.new(2020, 5, 16, 0, 0, 0)) # # => "UPDATE \"people\" SET \"updated_at\" = '2020-05-16 00:00:00'" # # # Touch records with scope - # Person.where(name: 'David').touch + # Person.where(name: 'David').touch_all # # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670' WHERE \"people\".\"name\" = 'David'" def touch_all(*names, time: nil) attributes = Array(names) + klass.timestamp_attributes_for_update_in_model |