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

has many through with non-lambda scope goes silently awry under activerecord 4.0.0.rc1 #10421

Closed
seanwalbran opened this issue May 2, 2013 · 2 comments

Comments

@seanwalbran
Copy link

In this gist: https://gist.github.com/seanwalbran/5501979
the query resulting from combining the has-many-through relationship with the named scopes behaves very differently depending on whether the scope is a lamba or not, as of 4.0.0.rc1:

I, [2013-05-02T07:52:37.973373 #55506]  INFO -- : with lambda
D, [2013-05-02T07:52:37.975057 #55506] DEBUG -- :    (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_mentions" ON "users"."id" = "user_mentions"."user_id" WHERE "user_mentions"."post_id" = ? AND "users"."admin" = 't'  [["post_id", 1]]
I, [2013-05-02T07:52:37.975166 #55506]  INFO -- : without lambda
D, [2013-05-02T07:52:37.976599 #55506] DEBUG -- :    (0.1ms)  SELECT COUNT(*) FROM "users" WHERE "users"."admin" = 't'

Note the complete absence of the join in the second query; up through 4.0.0.beta1, both queries were identical.

While I understand that the migration guide states that "Rails 4.0 requires that scopes use a callable object such as a Proc or lambda", the implementation only communicates that "Using #scope without passing a callable object is deprecated." The above behavior difference is sufficiently broken to be inconsistent with 'deprecation'.

@senny
Copy link
Member

senny commented May 7, 2013

@neerajdotname thoughts?

@neerajsingh0101
Copy link

Here is what's happening.

scoping { @klass.send(method, *args, &block) }

Above code is executed. However since the scope is not using block type the relation object is already created and that relation object does not make use of scoping.

I can force it by changing the code to

            r = @klass.send(method, *args, &block) 
            r.is_a?(ActiveRecord::Relation) : @klass.current_scope.merge(r) : r

However that is causing two tests to fail.

One potential issue is that I am applying same condition twice if scope is using block format.

Need to find a way to distinguish between the relations that are built lazily vs the relations which are built when class is loaded. I'll look into it more and will update my findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants