So picture the situations on the code below.
What I would like to do is that has_many(:permissions, through: [:account_roles, :role, :permissions])
only loads the permissions
that belongs to an active role (status: enabled)
So I want to be able to add some other where
clause in the has_many
association so developers can do preload:[:permissions]
without having to pay attention to the query in between, so it will aways give you the permissions that are active.
Right now has_many
constructs a query so I am guessing there is a way to do this because it is pretty much configure it with some query like preload
allow me to do it.
But I would love to know what do you think about the use cases (specially the core team of Ecto, because this can be a feature, or probably I shouldn’t be doing that anyway)
P.S: I know I could do the preload
with the query filtering the active roles
but the idea I am proposing is adding it to the has_many field
directly.
schema "accounts" do
has_many(:account_roles, AccountRole, on_delete: :delete_all)
has_many(:roles, through: [:account_roles, :role])
has_many(:permissions, through: [:account_roles, :role, :permissions])
end
schema "account_roles" do
belongs_to(:role, Role)
belongs_to(:account, Account)
timestamps()
end
schema "roles" do
field(:status, Status)
has_many(:account_roles, AccountRole, on_delete: :delete_all)
has_many(:role_permissions, RolePermission, on_delete: :delete_all)
has_many(:permissions, through: [:role_permissions, :permission])
end
schema "role_permissions" do
belongs_to(:role, Role)
belongs_to(:permission, Permission)
end
schema "permissions" do
field(:name, :string)
field(:service, :string)
end