Skip to content

Commit 6eae366

Browse files
committed
Deprecate force association reload by passing true
This is to simplify the association API, as you can call `reload` on the association proxy or the parent object to get the same result. For collection association, you can call `#reload` on association proxy to force a reload: @user.posts.reload # Instead of @user.posts(true) For singular association, you can call `#reload` on the parent object to clear its association cache then call the association method: @user.reload.profile # Instead of @user.profile(true) Passing a truthy argument to force association to reload will be removed in Rails 5.1.
1 parent 64c1264 commit 6eae366

File tree

9 files changed

+70
-0
lines changed

9 files changed

+70
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
* Deprecate force association reload by passing a truthy argument to
2+
association method.
3+
4+
For collection association, you can call `#reload` on association proxy to
5+
force a reload:
6+
7+
@user.posts.reload # Instead of @user.posts(true)
8+
9+
For singular association, you can call `#reload` on the parent object to
10+
clear its association cache then call the association method:
11+
12+
@user.reload.profile # Instead of @user.profile(true)
13+
14+
Passing a truthy argument to force association to reload will be removed in
15+
Rails 5.1.
16+
17+
*Prem Sichanugrist*
18+
119
* Replaced `ActiveSupport::Concurrency::Latch` with `Concurrent::CountDownLatch`
220
from the concurrent-ruby gem.
321

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "active_support/deprecation"
2+
13
module ActiveRecord
24
module Associations
35
# = Active Record Association Collection
@@ -28,6 +30,12 @@ class CollectionAssociation < Association #:nodoc:
2830
# Implements the reader method, e.g. foo.items for Foo.has_many :items
2931
def reader(force_reload = false)
3032
if force_reload
33+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
34+
Passing an argument to force an association to reload is now
35+
deprecated and will be removed in Rails 5.1. Please call `reload`
36+
on the result collection proxy instead.
37+
MSG
38+
3139
klass.uncached { reload }
3240
elsif stale_target?
3341
reload

activerecord/lib/active_record/associations/singular_association.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
require "active_support/deprecation"
2+
13
module ActiveRecord
24
module Associations
35
class SingularAssociation < Association #:nodoc:
46
# Implements the reader method, e.g. foo.bar for Foo.has_one :bar
57
def reader(force_reload = false)
68
if force_reload && klass
9+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
10+
Passing an argument to force an association to reload is now
11+
deprecated and will be removed in Rails 5.1. Please call `reload`
12+
on the parent object instead.
13+
MSG
14+
715
klass.uncached { reload }
816
elsif !loaded? || stale_target?
917
reload

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,12 @@ def test_reflect_the_most_recent_change
10641064
Column.create! record: record
10651065
assert_equal 1, Column.count
10661066
end
1067+
1068+
def test_association_force_reload_with_only_true_is_deprecated
1069+
client = Client.find(3)
1070+
1071+
assert_deprecated { client.firm(true) }
1072+
end
10671073
end
10681074

10691075
class BelongsToWithForeignKeyTest < ActiveRecord::TestCase

activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,4 +917,10 @@ def test_with_symbol_class_name
917917
DeveloperWithSymbolClassName.new
918918
end
919919
end
920+
921+
def test_association_force_reload_with_only_true_is_deprecated
922+
developer = Developer.find(1)
923+
924+
assert_deprecated { developer.projects(true) }
925+
end
920926
end

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,4 +2252,10 @@ def self.name
22522252

22532253
assert_equal [first_bulb, second_bulb], car.bulbs
22542254
end
2255+
2256+
def test_association_force_reload_with_only_true_is_deprecated
2257+
company = Company.find(1)
2258+
2259+
assert_deprecated { company.clients_of_firm(true) }
2260+
end
22552261
end

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,4 +1165,10 @@ def test_has_many_through_with_scope_that_should_not_be_fully_merged
11651165

11661166
assert_nil Club.new.special_favourites.distinct_value
11671167
end
1168+
1169+
def test_association_force_reload_with_only_true_is_deprecated
1170+
post = Post.find(1)
1171+
1172+
assert_deprecated { post.people(true) }
1173+
end
11681174
end

activerecord/test/cases/associations/has_one_associations_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,4 +607,10 @@ def test_with_polymorphic_has_one_with_custom_columns_name
607607
end
608608
end
609609
end
610+
611+
def test_association_force_reload_with_only_true_is_deprecated
612+
firm = Firm.find(1)
613+
614+
assert_deprecated { firm.account(true) }
615+
end
610616
end

activerecord/test/cases/associations/has_one_through_associations_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,10 @@ def test_has_one_through_relationship_cannot_have_a_counter_cache
344344
end
345345
end
346346
end
347+
348+
def test_association_force_reload_with_only_true_is_deprecated
349+
member = Member.find(1)
350+
351+
assert_deprecated { member.club(true) }
352+
end
347353
end

0 commit comments

Comments
 (0)