Need to ensure that only 1 row for a query criteria has a specific column value
I have a Campaign and that Campaign has multiple Crew Members. Only one of these Crew Members can have the is_leader column set to true. I think what I need is to be able to
UPDATE crew_members SET is_leader = true WHERE campaign_id = $1, and then update the specific Crew Member. I haven't really seen the ideomatic way of doing this in Ash.7 Replies
Maybe this is what you're looking for: https://hexdocs.pm/ash/dsl-ash-resource.html#identities-identity-where
I think that's only part of the problem. The specific part I'm struggling with is when I want to switch which crew member is the leader.
identities just helps enforce uniqueness, right?
yes that's what they do.
I would use a bulk_update in my action that sets every person that is
true in that campain to false, in a before_action.Here's what I came up with but it feels super heavy-handed:
I see. just a few updates on your clrea_other_leaders. You don't have to do Enum.each you can batch the update
And you don't even need to read with bulk_updates
try this, I think it should work and be more performant.
Hm, I might even be able to make it simpler with a specific action.
Like if I had
update :set_leader dosorry?
Does the code you sent works currently? When I see you have an after_action that sets all leaders
true to false.
I see. You filter out the current_crew_member in the query. Should be good.