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

Casting drops the strict_loading state of an ActiveRecord object #181

Closed
makmic opened this issue Nov 17, 2023 · 2 comments · Fixed by #182
Closed

Casting drops the strict_loading state of an ActiveRecord object #181

makmic opened this issue Nov 17, 2023 · 2 comments · Fixed by #182

Comments

@makmic
Copy link
Member

makmic commented Nov 17, 2023

I was recently faced with N+1 queries which surprised me as I had strict_loading enabled in every controller.
Digging into the issue I noticed that active_type drops the strict_loading state after casting an object:

user = User.strict_loading.find(3)
user.strict_loading? # true

casted_user = ActiveType.cast(user, User::Form)
casted_user.strict_loading? # false

A quick workaround would be to re-enable it after casting:

casted_user = ActiveType.cast(user, User::Form).tap(&:strict_loading!) if user.strict_loading?
casted_user.strict_loading? # true

I think it would make sense to fix this in the gem itself, WDYT?

@kratob
Copy link
Member

kratob commented Nov 20, 2023

Yes, that's definitely something ActiveType should fix.

@makmic
Copy link
Member Author

makmic commented Nov 20, 2023

Okay. 👍

I just had another look at all boolean methods of an example AR-instance. We should probably check all of those while fixing the issue above:

 :strict_loading?,
 :strict_loading_n_plus_one_only?,
 :readonly?,
 :frozen?,

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

Successfully merging a pull request may close this issue.

2 participants