https://avo.cool logo
👋 Hey everyone! I have a situation like this:...
# avo-2
m
Thread automatically created by stevent4520 in #740893011994738751
l
Hello @fancy-crowd-87619 the Organization default scope is applying and when
belongs_to
field is rendered the query will be affected by it.
field :organization, as: :belongs_to, attach_scope: -> { query.unscoped }
Using the
attach_scope
option to unscope the query should work in this scenario. https://docs.avohq.io/2.0/associations/belongs_to.html#belongs-to-attach-scope
f
Thanks Paul - unfortunately that only gets me halfway there - with
Copy code
field :nonprofit, as: :belongs_to, readonly: true, attach_scope: -> { query.unscoped }
I get "Nonprofit must exist" when I try to save the record. Any thoughts on this one? Is there another scope modifier for saving? https://cdn.discordapp.com/attachments/1166882659939647558/1167169320451846174/Screenshot_2023-10-26_at_11.32.48_AM.png?ex=654d2622&is=653ab122&hm=655562c26a572534f0fc9a9925313bae1b974d3cd04867743eab484059a04fdc&
("Organization" == "Nonprofit", sorry 🙂 )
l
I think we already fixed this issue on Avo3, I can check the issue better tomorrow, until there if it's blocking try to use
disabled
instead
readonly
. Notice that
disabled
allow bad actors to change the
nonporift
value on the DOM and update that field on form submit.
f
Same issue with disabled - not urgent on my end, I can wait for Avo 3. Thanks for your help!
Oh interesting. Even if I remove the belongs_to field it won't save. I guess the scope is critical here
hmm
l
I played arround with a similar scenario and it looks like the ActiveRecord will not find a record that is excluded from the scope. In my example I have
Course::Link
tha belongs to
Course
. All links have a course but if I add a scope to
Course
model, something like
default_scope { where(id: 1...5) }
and then go to a rails console and pick a
Course::Link
that i know it belongs to a
Course
with id bigger than 5 it will not found the course for the link (
Course::Link.find(404).course
returns nil when the defaults cope is applyed on
Course
and return the proper
Course
when i remove the scope)
What's happening is that "Orca Conservancy" can't be found by Active Record since is out of Nonprofit default scope
f
I appreciate you taking the time to play around with it. I'm familiar with default scopes, and chain
unscoped
to the query when I need to escape the default scope. E.g.
Course::Link.unscoped.find(404)
works. Default scopes haven't caused me any trouble until now it seems. Whatever queries Avo is using to save the record is not applying
unscoped
when fetching the related records (is my guess). We may have to remove our default scopes unless Avo will inject
unscoped
everywhere - which I would definitely not expect of you all
l
Injecting
unscoped
on Avo codebase is not a viable expectation, there are multi tenancy scenarios where default scopes are desired for "triage" records. One posibility is to add a hook and make possible to apply a custom query,
query.unscoped
in your case
f
Completely understand - I do not expect this
Interesting idea - thank you so much for all of your help!
l
https://github.com/avo-hq/avo/issues/2005 Anytime! I created this issue
3 Views