What is the best practice for `Ash.can?/3`

Here’s the my example.

I’m working with structure where a Room has_one Contract, and I want to enforce that a contract can only be created when the room is unsold. However, when using Ash.can?({Contract, :create}, actor), I can’t access the parent room’s state. So, I thought it would be necessary to verify the policy through Ash.can?({Room, :contract}, actor). For this reason, I’ve adjusted the Room’s contract action to take :contract_params in a map. This adjustment has led me to wrap my form inputs with inputs_for.

I’m not entirely happy with this solution and am unsure if there’s a better method. I would like to hear your thoughts or any alternatives.

It’s an interesting question. This works for updates because you have the Contract. You could try doing something like Ash.can?({%Contract{room: %Room{}}, :create}), but I’m not sure if the can? code will check for that on create.

1 Like

Thank you for your response. I tried to apply the method you mentioned, I haven’t been able to apply it yet due to my limited understanding of Ash.can?.