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

with_deleted unscopes everything when joining through multiple associations #282

Open
heaven opened this issue Jun 8, 2022 · 3 comments

Comments

@heaven
Copy link

heaven commented Jun 8, 2022

We have Subscriptions, Products, Prices, and AddOns.

class Payments::Subscription < ApplicationRecord
  has_many :subscription_addons,
    class_name: "Payments::SubscriptionAddon", inverse_of: :subscription, dependent: :destroy

  has_many :addons, class_name: "Payments::Product", through: :subscription_addons, source: :product
end

class Payments::SubscriptionAddon < ApplicationRecord
  belongs_to :price, -> { with_deleted },
    class_name: "Payments::Price", inverse_of: :subscription_addons

  has_one :product, class_name: "Payments::Product", through: :price
end

class Payments::Price < ApplicationRecord
  belongs_to :product, -> { with_deleted },
    class_name: "Payments::Product", inverse_of: :prices
end

When I access @subscription.subscription_addons everything is great and it returns only SubscriptionAddons where deleted_at is null.

But when I need to access @subscription.addons, the response also includes Products joined through already deleted SubscriptionAddons. In other words with_deleted that I apply to belongs_to :product/:price also applies to has_many :subscription_addons even thought it wasn't explicitly declared there. Basically, it unscopes where(::deleted_at) where it shouldn't.

@heaven heaven changed the title with_deleted unscopes everything when joining multiple associations with_deleted unscopes everything when joining through multiple associations Jun 8, 2022
@kroehre
Copy link

kroehre commented Jul 13, 2022

I just encountered this as well. @heaven were you able to find a workaround?

@heaven
Copy link
Author

heaven commented Jul 13, 2022

@kroehre nope and I don't think there is an easy fix, unfortunately. In my case, I simply stopped using acts_as_paranoid on SubscriptionAddon, which is just a :through model between Subscription and Price. This allowed me to work without with_deleted in associations. Also, building propper foreign key constraints and validations prevents deletions of a Price with active Subscriptions so this works somehow. Not as planned, though.

@kroehre
Copy link

kroehre commented Jul 18, 2022

@mvz could this be related to #277 ? Issue persists after downgrading to 0.8.0

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

2 participants