提交 5f1346b0 编写于 作者: R Rafael Mendonça França

Merge pull request #14562 from jefflai2/collection_proxy

CollectionProxy uses the arel of its association's scope.

Conflicts:
	activerecord/CHANGELOG.md
* `to_sql` on an association now matches the query that is actually executed, where it
could previously have incorrectly accrued additional conditions (e.g. as a result of
a previous query). CollectionProxy now always defers to the association scope's
`arel` method so the (incorrect) inherited one should be entirely concealed.
Fixes #14003.
*Jefferson Lai*
* Block a few default Class methods as scope name.
For instance, this will raise:
......
......@@ -860,6 +860,10 @@ def include?(record)
!!@association.include?(record)
end
def arel
scope.arel
end
def proxy_association
@association
end
......
......@@ -573,6 +573,12 @@ def test_to_sql_on_eager_join
assert_equal expected, actual
end
def test_to_sql_on_scoped_proxy
auth = Author.first
Post.where("1=1").written_by(auth)
assert_not auth.posts.to_sql.include?("1=1")
end
def test_loading_with_one_association_with_non_preload
posts = Post.eager_load(:last_comment).order('comments.id DESC')
post = posts.find { |p| p.id == 1 }
......
......@@ -149,6 +149,10 @@ def self.top(limit)
ranked_by_comments.limit_by(limit)
end
def self.written_by(author)
where(id: author.posts.pluck(:id))
end
def self.reset_log
@log = []
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册