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

Any way to unscope join association #110

Open
vitalinfo opened this issue Sep 27, 2018 · 2 comments
Open

Any way to unscope join association #110

vitalinfo opened this issue Sep 27, 2018 · 2 comments

Comments

@vitalinfo
Copy link

class User < ApplicationRecord
  acts_as_paranoid
  has_many :user_settings
end

class UserSetting < ApplicationRecord
  acts_as_paranoid
  belongs_to :user
end

I haven't found any solution to exclude "user_settings"."deleted_at" IS NULL from the query.

Model.unscoped doesn't help

UserSetting.unscoped { User.with_deleted.joins(:user_settings) }
SELECT  "users".* FROM "users" INNER JOIN "user_settings" ON "user_settings"."user_id" = "users"."id" AND "user_settings"."deleted_at" IS NULL LIMIT $1
@heaven
Copy link

heaven commented Apr 11, 2021

I did the following:

belongs_to :plan, -> { unscope(where: :deleted_at) }, foreign_key: :plan_slug, primary_key: :slug

The query has changed from this:

SELECT `plans`.* FROM `plans` WHERE `plans`.`deleted_at` IS NULL AND `plans`.`slug` = 'executive' LIMIT 1

To this:

SELECT `plans`.* FROM `plans` WHERE `plans`.`slug` = 'executive' LIMIT 1

@mvz mvz changed the title Any way to unscope join association (Rails 5.2.0) Any way to unscope join association Jul 28, 2024
@mvz
Copy link
Contributor

mvz commented Jul 28, 2024

Summarizing, it seems what is desired is a way to have a query like

User.joins(:user_setting)

but with deleted user_settings included.

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

3 participants