How to have a "virtual field" in a resource based from another resource?
I have a
Organization
resource that has a many to many relationship with my User
resource.
That relationship is defined by this field in the Organization
resource:
The UserOrganization
resource has a special attribute inside of which defined if the user is an admin inside that organization:
What I want to do is to create a virtual attribute
in the Organization
resource that will tell if that user (the actor) is an admin.
My first approach was to add this new relationship to the resource:
So I can get access to the UserOrganization
resource directly from Organization
.
After that, I tried creating a calculation that would join that relationship and "extract" the admin?
field:
That don't work, and I'm not sure how exactly I would filter that calculation to only get the user_organization
from the actor.
My second approach was to try an aggregate:
This also doesn't work, and aggregates also will generate a more complex query instead of simply doing the join by organization_id
and user_id
.
Any suggestion on how I can acchieve that?16 Replies
🤔
I can add an option for this
we already have the concept of doing simple joins for
first
aggregates that go over only to_one
relationships
so all we really need to do is expose that as an option
for cases where we can't tellThat would be great!
Right now my workaround is to have:
instead of
and do
instead of
Having that would allow me to make things simpler IMO
oh, actually nevermind, I just realized you are looking for this to be scoped to the actor
calculate :admin?, :boolean, expr(exists(user_organizations, user_id == ^actor(:id) and admin? == true))
thats what you want I thinkNot sure, if I try to load that calculation with:
I get:
oh, sorry
Either way, I think I would not be able to use it anyway since I want to load it when I get my user, meanign that I don't have an actor yet
do you have a
user_organizations
relationship?Yep
add that
join_relationship
I was referring to a relationship you don't have in my example which was the cause of the error. Add the
join_relationship
option I mentioned above
You don't have an actor yet, but you do have the user's id right?
or not yet?Yep, I want to do that when ash_authenticate fetches the user
So I actually have the user email/pwd haha
The calculation worked after I added the
join_relationship
btw.Can you move this to be on the user instead?
You could put that on the user to see what organizations they are an admin of
I could, but that means that when I wan´t to check for that information, I need to check in two places.
Let's say I'm creating a page where I show a list of the user organizations, I would need to check, for each organization, if it is also inside the
admin_of
list in the user
Btw, I think I found a but, not sure.
If I remove the admin? == true
from the calculation, it works. If I add that back, I get this error:
uh....woah
alright, well, I'll have to look at that tomorrrow
Your best bet is probably to do one action to fetch the user w/o loading any data, and then use
Api.load
on that user to fetch all of the related things you want, with that user as the actor
It would only really add a few ms
to the request, and then you can use policies and stuff to enforce what that user can/can't accessGot it, makes sense, I will try that and see how it goes