-
-
Notifications
You must be signed in to change notification settings - Fork 267
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
Swallowing ActiveRecord::RecordNotDestroyed exceptions #253
Comments
Perhaps acts_as_tenant has a https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-destroy-21 |
Some more information. I replicated this error surfaced by a system test in a unit test. Sooo...
Hence, I see the InvalidForeignKey exception and not the originally anticipated RecordNotDestroyed exception. By turning off the tenant:
I indeed see the RecordNotDestroyed exception again. In addition, [1] I likely need to have the tenant turned off anyway in order for my cascading deletes to occur! I don't yet have this situation tested but am going to add it to verify what I am observing here. Thanks for being my duck! Will close once I verify. |
Hmmm... interesting. Per [1] marked in the above comment, I am actually seeing some unexpected behavior, so I will describe it here, see if others chime in, and ya'll can decide to close this or open a more accurate issue. With acts_as_tenant, a Consider a School that has_many Activities:
And an Activity that has_many Registrations:
Here is a test, don't worry about the context/purpose, just notice that it sets a tenant, then invokes a delete on a different object:
Notice the SQL in the test.log output: Notice how the Activity Load query uses the id of Notice how the Registration Load query uses the id of the current tenant, and not the id of My question is: have I uncovered a bug in acts_as_tenant? (If so, please create a new issue and close this one.) If this is expected behavior, can anyone explain why? (Thank you.) |
What I am suspicious of, if this is a bug, is that acts_as_tenant is either not properly scoping SELECTs for first-degree associated records upon delete. Or that acts_as_tenant should not scope SELECTS for second-degree associated records upon delete. |
Is |
Yep.
|
That's why then since the default scope will set it to the second school automatically. |
@excid3 Right.... but I am surprised that, in the query log screenshotted above, while the "Registration Load" does use the tenant, the query for "Activity Load" doesn't use the tenant. |
Is Activity also acts_as_tenant? If you say school.activities, it will set the Then the destroy would call |
Yep, Activity is also acts_as_tenant:
I agree with what you are observing. My question is: isn't this "surprising" or "inconsistent" behavior that acts_as_tenant may have overlooked in its implementation? Or... I think I see what's happening:
|
Yes and no. The I think it makes sense that if you expect to destroy a tenant and it's records, it makes sense that you'd want to be in that tenant and not another one. What you're doing is saying:
I agree that it may seem a bit unexpected, but if this were relaxed then you could view School 1's records from the School 2 tenant by using the associations to access records. That would be bad. |
See the comments in school_test.rb. See ErwinM/acts_as_tenant#253.
Hi and thanks for all things acts_as_tenant. I am trying it out on an existing legacy project, and I seem to be observing behavior that is somehow interfering with the default ActiveRecord exception throwing. It appears like acts_as_tenant is swallowing an ActiveRecord::RecordNotDestroyed exception.
Here's the model for my tenant: a School.
In the PostgreSQL schema, I also have FK constraints on the foreign keys for each of the above associations.
In addition, one of those associated models, Teacher, has a
dependent: :restrict_with_error
on its other associations.In short, I can destroy a School and anything associated with it, as long as an associated Teacher does not have students or registrations associated with it.
Consider the
destroy
method in my SchoolsController:Before I introduced acts_as_tenant, destroying a School that is not destroyable, due to some associated restrictions/constraints, would raise an ActiveRecord::RecordNotDestroyed exception.
After I introduced acts_as_tenant, this method does not raise the ActiveRecord::RecordNotDestroyed exception. Instead, the underlying ActiveRecord::InvalidForeignKey exception gets raised. I would expect to see this exception be raised due to a deletion that violates the FK constraints - this would occur if I did not declare the
dependent
options on the associations.If I change the
destroy
action to ignore the tenant:Then I indeed see the ActiveRecord::RecordNotDestroyed exception.
I wonder - might acts_as_tenant be somehow swallowing the RecordNotDestroyed exception, or somehow interfering with my
dependent
options? This is all under test, and I believe that in my test I am indeed using fixtures that include only tenant-specific models. (But I am triple-checking this now.)The text was updated successfully, but these errors were encountered: