Skip to content

Commit

Permalink
Merge pull request #14562 from jefflai2/collection_proxy
Browse files Browse the repository at this point in the history
CollectionProxy uses the arel of its association's scope.

Conflicts:
	activerecord/CHANGELOG.md

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/cases/relations_test.rb
  • Loading branch information
rafaelfranca committed Apr 4, 2014
1 parent c5af132 commit b30104c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,12 @@
* `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*

* Fixed error when using `with_options` with lambda. * Fixed error when using `with_options` with lambda.


Fixes #9805. Fixes #9805.
Expand Down
Expand Up @@ -833,6 +833,10 @@ def include?(record)
!!@association.include?(record) !!@association.include?(record)
end end


def arel
scope.arel
end

def proxy_association def proxy_association
@association @association
end end
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -503,6 +503,12 @@ def test_loading_with_one_association
assert_equal Post.find(1).last_comment, post.last_comment assert_equal Post.find(1).last_comment, post.last_comment
end 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 def test_loading_with_one_association_with_non_preload
posts = Post.eager_load(:last_comment).order('comments.id DESC') posts = Post.eager_load(:last_comment).order('comments.id DESC')
post = posts.find { |p| p.id == 1 } post = posts.find { |p| p.id == 1 }
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/models/post.rb
Expand Up @@ -142,6 +142,10 @@ def self.top(limit)
ranked_by_comments.limit_by(limit) ranked_by_comments.limit_by(limit)
end end


def self.written_by(author)
where(id: author.posts.pluck(:id))
end

def self.reset_log def self.reset_log
@log = [] @log = []
end end
Expand Down

0 comments on commit b30104c

Please sign in to comment.