提交 5706d290 编写于 作者: Y Yves Senn

`preload` preserves readonly flag on associations. #15853

This is a partial fix for #15853. It only works when a `preload`
is issued and not an `eager_load`. I've added a skipped failing
test-case to keep in mind that we need to deal with `eager_load`.
上级 48933542
* `preload` preserves readonly flag for associations.
See #15853.
*Yves Senn*
* Assume numeric types have changed if they were assigned to a value that
would fail numericality validation, regardless of the old value. Previously
this would only occur if the old value was 0.
......
......@@ -151,6 +151,10 @@ def build_scope
end
end
if preload_values[:readonly] || values[:readonly]
scope.readonly!
end
if options[:as]
scope.where!(klass.table_name => { reflection.type => model.base_class.sti_name })
end
......
......@@ -1261,4 +1261,33 @@ def test_deep_including_through_habtm
Author.eager_load(:posts_with_signature).to_a
end
end
test "preloading readonly association" do
# has-one
firm = Firm.where(id: "1").preload(:readonly_account).first!
assert firm.readonly_account.readonly?
# has_and_belongs_to_many
project = Project.where(id: "2").preload(:readonly_developers).first!
assert project.readonly_developers.first.readonly?
# has-many :through
david = Author.where(id: "1").preload(:readonly_comments).first!
assert david.readonly_comments.first.readonly?
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?
# has_and_belongs_to_many
project = Project.where(id: "2").eager_load(:readonly_developers).first!
assert project.readonly_developers.first.readonly?
# has-many :through
david = Author.where(id: "1").eager_load(:readonly_comments).first!
assert david.readonly_comments.first.readonly?
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册