aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-12-29 14:18:54 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-12-29 14:18:54 -0300
commit266ff700011dfcc4b76a7332bbfa64d9419c10bc (patch)
tree486ddf150e580c3381c5a807a6de94aa5e5abaa8
parent0906e8322b81c429fddeb89ab08c279719bf29b0 (diff)
parent068160b444a5ceccd0bdc5dd81b70bc45754bcaa (diff)
downloadrails-266ff700011dfcc4b76a7332bbfa64d9419c10bc.tar.gz
rails-266ff700011dfcc4b76a7332bbfa64d9419c10bc.tar.bz2
rails-266ff700011dfcc4b76a7332bbfa64d9419c10bc.zip
Merge pull request #18097 from k0kubun/readonly-eager_load
`eager_load` preserves readonly flag for associations
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/associations/join_dependency.rb1
-rw-r--r--activerecord/test/cases/associations/eager_test.rb5
3 files changed, 11 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index d9966f0ad7..6a1bdcb86d 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* `eager_load` preserves readonly flag for associations.
+
+ Closes #15853.
+
+ *Takashi Kokubun*
+
* Provide `:touch` option to `save()` to accommodate saving without updating
timestamps.
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb
index cf63430a97..6c1c700d54 100644
--- a/activerecord/lib/active_record/associations/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/join_dependency.rb
@@ -257,6 +257,7 @@ module ActiveRecord
construct(model, node, row, rs, seen, model_cache, aliases)
else
model = construct_model(ar_parent, node, row, model_cache, id, aliases)
+ model.readonly!
seen[parent.base_klass][primary_id][node.base_klass][id] = model
construct(model, node, row, rs, seen, model_cache, aliases)
end
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index db8fd92c1f..fdb437d11d 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -1328,7 +1328,6 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
test "eager-loading readonly association" do
- skip "eager_load does not yet preserve readonly associations"
# has-one
firm = Firm.where(id: "1").eager_load(:readonly_account).first!
assert firm.readonly_account.readonly?
@@ -1340,6 +1339,10 @@ class EagerAssociationTest < ActiveRecord::TestCase
# has-many :through
david = Author.where(id: "1").eager_load(:readonly_comments).first!
assert david.readonly_comments.first.readonly?
+
+ # belongs_to
+ post = Post.where(id: "1").eager_load(:author).first!
+ assert post.author.readonly?
end
test "preloading a polymorphic association with references to the associated table" do