acts_as_tenant icon indicating copy to clipboard operation
acts_as_tenant copied to clipboard

Honor the scope passed to associations when performing validations

Open adrian-gomez opened this issue 2 years ago • 1 comments

Hi @ErwinM, thanks for the awesome gem!

We recently started to migrate our whole app to use acts_as_tenant and found a small issue for our use case where validations performed by acts_as_tenant are failing when checking for the existence of belongs_to associations. We have been using https://github.com/rubysherpas/paranoia for a long time (we have plans to migrate away from it in the future but it's not an easy task). So the issue is the default scope generated by paranoia which makes deleted records to be skipped during validation, that makes sense since those are deleted! but in some scenarios we do want our models to access deleted associations, the way to do that is to pass a scope to the association:

belongs_to :other, -> { with_deleted }

right now acts_as_tenant is ignoring that scope so the validation fails.

The PR will use the scope if given.

An extra addition (that I can move to another PR if you like) is allowing to pass a scope to act_as_tenant and just pass the scope directly to belongs_to (it seems that's the only modification needed for that to work)

adrian-gomez avatar May 06 '22 13:05 adrian-gomez

@excid3 do you think this is something you might want to add to the gem? As we keep using we are coming across some other changes we would like to suggest/implement is a PR the best way to do those? or should we open an issue first? Thanks!

adrian-gomez avatar May 17 '22 12:05 adrian-gomez

Thanks for this @adrian-gomez

acts_as_tenant should match the belongs_to interface since it defines the same thing behind the scenes so it would be great to support scopes. 👍

Would you be able to fix the merge conflicts and I can merge this in?

excid3 avatar Dec 02 '22 01:12 excid3