Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve readonly flag only for readonly association #24099

Merged
merged 1 commit into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
construct(model, node, row, rs, seen, model_cache, aliases)
else
model = construct_model(ar_parent, node, row, model_cache, id, aliases)
model.readonly!
if node.reflection.scope_for(node.base_klass).readonly_value
model.readonly!
end
seen[ar_parent.object_id][node.base_klass][id] = model
construct(model, node, row, rs, seen, model_cache, aliases)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def load_records
end

def reflection_scope
@reflection_scope ||= reflection.scope ? klass.unscoped.instance_exec(nil, &reflection.scope) : klass.unscoped
@reflection_scope ||= reflection.scope_for(klass)
end

def build_scope
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ def ==(other_aggregation)
active_record == other_aggregation.active_record
end

def scope_for(klass)
scope ? klass.unscoped.instance_exec(nil, &scope) : klass.unscoped
end

private
def derive_class_name
name.to_s.camelize
Expand Down
22 changes: 20 additions & 2 deletions activerecord/test/cases/associations/eager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,24 @@ def test_eager_load_multiple_associations_with_references
assert david.readonly_comments.first.readonly?
end

test "eager-loading non-readonly association" do
# has_one
firm = Firm.where(id: "1").eager_load(:account).first!
assert_not firm.account.readonly?

# has_and_belongs_to_many
project = Project.where(id: "2").eager_load(:developers).first!
assert_not project.developers.first.readonly?

# has_many :through
david = Author.where(id: "1").eager_load(:comments).first!
assert_not david.comments.first.readonly?

# belongs_to
post = Post.where(id: "1").eager_load(:author).first!
assert_not post.author.readonly?
end

test "eager-loading readonly association" do
# has-one
firm = Firm.where(id: "1").eager_load(:readonly_account).first!
Expand All @@ -1438,8 +1456,8 @@ def test_eager_load_multiple_associations_with_references
assert david.readonly_comments.first.readonly?

# belongs_to
post = Post.where(id: "1").eager_load(:author).first!
assert post.author.readonly?
post = Post.where(id: "1").eager_load(:readonly_author).first!
assert post.readonly_author.readonly?
end

test "preloading a polymorphic association with references to the associated table" do
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def greeting
scope :limit_by, lambda {|l| limit(l) }

belongs_to :author
belongs_to :readonly_author, -> { readonly }, class_name: 'Author', foreign_key: :author_id

belongs_to :author_with_posts, -> { includes(:posts) }, :class_name => "Author", :foreign_key => :author_id
belongs_to :author_with_address, -> { includes(:author_address) }, :class_name => "Author", :foreign_key => :author_id
Expand Down