rails/rails — pull request #10528 by neerajdotname (2013-05-09 01:19:14+00:00)
Title: handle non lambda scope properly

This PR is only for 4-0-stable since in master scope without lambda is no longer supported.

I will add tests and change log if the fix looks ok.

fixes #10421

If `@post.comments.approved_only_without_lambda.count` is invoked
then the result is equivalent to calling
`Comment.approved_only_without_lambda`.

This is because when `scope` is defined without lambda then `scope`
stores an already built relation.

`@post.comments.approved_only_without_lambda` invokes the method
using `scoping`.

`scoping` changes the `current_scope` so that the relation that is
to built can make use of the `current_scope`.

However if `scope` is defined without lambda then the relation is
already built and I could not find anyway to apply current_scope
on an already built relation.

If I use `merge` then last where condition wins and that was removing
conditions from `current_scope`.

Hence I added a way to merge two relations without anyone clobbering
on another.

All existing tests are passing.

/cc @jonleighton