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

[Rails 5.0] Unscope for the join #30534

Closed
Riddlerrr opened this issue Sep 6, 2017 · 3 comments
Closed

[Rails 5.0] Unscope for the join #30534

Riddlerrr opened this issue Sep 6, 2017 · 3 comments

Comments

@Riddlerrr
Copy link

Riddlerrr commented Sep 6, 2017

We have 2 models:

class DeveloperList < ApplicationRecord
  has_many :developers, -> { unscope(where: :deleted_at) }
end

class Developer < ApplicationRecord
  belongs_to :developer_list
  default_scope { where(deleted_at: nil) }
end

With simple using has_many it works as expected:

>   DeveloperList.first.developers.count
  DeveloperList Load (0.5ms)  SELECT  "developer_lists".* FROM "developer_lists" ORDER BY "developer_lists"."id" ASC LIMIT ?  [["LIMIT", 1]]
   (0.5ms)  SELECT COUNT(*) FROM "developers" WHERE "developers"."developer_list_id" = ?  [["developer_list_id", 1]]
 => 2

But with join it doesn't work.

> DeveloperList.joins(:developers).count
   (0.5ms)  SELECT COUNT(*) FROM "developer_lists" INNER JOIN "developers" ON "developers"."developer_list_id" = "developer_lists"."id" AND "developers"."deleted_at" IS NULL
 => 1 # should be 2

That because in sql still leave the AND "developers"."deleted_at" IS NULL.

Rails 5.0.6.rc1 Ruby 2.2.3

In Rails 5.1.4.rc1 (latest 5-1-stable branch, not release) it works as expected.
I think we need to backport fix from 5.1 rails to 5.0 too.

@alexcameron89
Copy link
Member

@kamipo It looks like c9cf8b8 was backported to 5-1-stable but may need to be backported to5-0-stable as well?

kamipo added a commit that referenced this issue Sep 6, 2017
Backport of #29611.

Unscoping `default_scope` in associations has already supported (#17360
for preloading, c9cf8b8 for eager loading). But it is hard to backport
c9cf8b8 to 5-0-stable, so I only picked fixing merging scopes order.

Closes #30534.
@kamipo
Copy link
Member

kamipo commented Sep 6, 2017

Yeah, backported in 9d364cc.

@kamipo kamipo closed this as completed Sep 6, 2017
@estani
Copy link

estani commented Aug 28, 2019

This still doesn't work if a where clause is added (afaic only count works).

default_scope adds the current tenant:

Billing.unscoped { Tenant.joins(:billing).count }
SELECT COUNT(*) FROM "tenants" INNER JOIN "billings" ON "billings"."tenant_id" = "tenants"."id"

no tenant id, unscoped is working here.

Billing.unscoped { Tenant.joins(:billing).where(billings: {state: 1})}
SELECT  "tenants".* FROM "tenants" INNER JOIN "billings" ON "billings"."tenant_id" = "tenants"."id" AND "billings"."tenant_id" = $1 WHERE "billings"."state" = $2 ORDER BY "tenants"."id" ASC LIMIT $3 

All those extra conditions show that unscoped is not being honored here.

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

No branches or pull requests

4 participants