harry
harry
AEAsh Elixir
Created by harry on 7/1/2023 in #support
relationship required
Hey! Whats the best way to fix this error?
%Ash.Error.Changes.Required{
field: :account,
type: :relationship,
resource: nil,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
%Ash.Error.Changes.Required{
field: :account,
type: :relationship,
resource: nil,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
I already tried including account_id and account in the changeset, this is what I have currently
Vehicle
|> Ash.Changeset.for_create(
:create,
%{
account: account,
....
}
)
|> AshApi.create([])
Vehicle
|> Ash.Changeset.for_create(
:create,
%{
account: account,
....
}
)
|> AshApi.create([])
4 replies
AEAsh Elixir
Created by harry on 6/5/2023 in #support
Querying JSON fields
I have this query,
SELECT *
FROM users
WHERE settings->>'reminder_day' = 'Monday' -- Replace 'Monday' with your specific day
AND (settings->>'reminder_enabled')::boolean = true
AND (settings->>'reminder_time')::time BETWEEN '09:00'::time AND ('09:00'::time + INTERVAL '1 hour');
SELECT *
FROM users
WHERE settings->>'reminder_day' = 'Monday' -- Replace 'Monday' with your specific day
AND (settings->>'reminder_enabled')::boolean = true
AND (settings->>'reminder_time')::time BETWEEN '09:00'::time AND ('09:00'::time + INTERVAL '1 hour');
and I'd like to convert it to an Ash query - any suggestions?
# User.ex
attribute :settings, Proj.Resources.Accounts.UserSettings do
allow_nil?(false)
# TODO, can this be improved?!
default(fn -> %Proj.Resources.Accounts.UserSettings{} end)
end

# UserSettings.ex
defmodule Proj.Resources.Accounts.UserSettings do
use Ash.Resource,
data_layer: :embedded

attributes do
attribute :time_zone, :string do
allow_nil?(false)
default("Europe/London")
end

attribute :reminder_enabled, :boolean do
allow_nil?(false)
default(true)
end

attribute :reminder_time, :string do
allow_nil?(false)
default("09:00")
end

attribute :reminder_day, :string do
allow_nil?(false)
default("Monday")
end
end
end
# User.ex
attribute :settings, Proj.Resources.Accounts.UserSettings do
allow_nil?(false)
# TODO, can this be improved?!
default(fn -> %Proj.Resources.Accounts.UserSettings{} end)
end

# UserSettings.ex
defmodule Proj.Resources.Accounts.UserSettings do
use Ash.Resource,
data_layer: :embedded

attributes do
attribute :time_zone, :string do
allow_nil?(false)
default("Europe/London")
end

attribute :reminder_enabled, :boolean do
allow_nil?(false)
default(true)
end

attribute :reminder_time, :string do
allow_nil?(false)
default("09:00")
end

attribute :reminder_day, :string do
allow_nil?(false)
default("Monday")
end
end
end
4 replies
AEAsh Elixir
Created by harry on 5/1/2023 in #support
`many_to_many` error
** (RuntimeError) Resource `SwiftEco.Resources.Accounts.UsersCompanies` is not in registry `SwiftEco.Resources.Companies` for autogenerated join relationship: `:users_join_assoc`

Relationship was generated by the `many_to_many` relationship `:users`

If the `through` resource `SwiftEco.Resources.Accounts.UsersCompanies` is not accepted by the same
api as the destination resource `SwiftEco.Resources.Accounts.User`,
then you must define that relationship manually. To define it manually, add the following to your
relationships:

has_many :users_join_assoc, SwiftEco.Resources.Accounts.UsersCompanies do
# configure the relationship attributes
...
end

You can use a name other than `:users_join_assoc`, but if you do, make sure to
add that to `:users`, i.e

many_to_many :users_join_assoc, SwiftEco.Resources.Accounts.UsersCompanies do
...
join_relationship_name :your_new_name
end
** (RuntimeError) Resource `SwiftEco.Resources.Accounts.UsersCompanies` is not in registry `SwiftEco.Resources.Companies` for autogenerated join relationship: `:users_join_assoc`

Relationship was generated by the `many_to_many` relationship `:users`

If the `through` resource `SwiftEco.Resources.Accounts.UsersCompanies` is not accepted by the same
api as the destination resource `SwiftEco.Resources.Accounts.User`,
then you must define that relationship manually. To define it manually, add the following to your
relationships:

has_many :users_join_assoc, SwiftEco.Resources.Accounts.UsersCompanies do
# configure the relationship attributes
...
end

You can use a name other than `:users_join_assoc`, but if you do, make sure to
add that to `:users`, i.e

many_to_many :users_join_assoc, SwiftEco.Resources.Accounts.UsersCompanies do
...
join_relationship_name :your_new_name
end
Happy to share any code needed just not sure what the actual error means! It was working before and I have just started work on tidying up the codebase and this is now an error that has appeared
11 replies
AEAsh Elixir
Created by harry on 3/4/2023 in #support
How to run something in the background after action?
Hi, I tried using notifiers but it looks like they block. Is there a way to run something in the background that is triggered by an actions e.g. user_created, etc
6 replies
AEAsh Elixir
Created by harry on 2/20/2023 in #support
Insert into Database without Form
Is there a way to insert into the database a new entry without a form. i.e. a user does a form action which creates an entity and then I need to update the user to attach them to that new entity.
13 replies
AEAsh Elixir
Created by harry on 2/16/2023 in #support
Does Ash Auth support Google/Apple OAuth?
I think the title sums it up 😄
6 replies
AEAsh Elixir
Created by harry on 1/28/2023 in #support
Autogenerated unique short IDs
What is the best way in Ash to generate short IDs e.g. invite codes that are unique and will keep generating till it gets a unique one? Actions?
18 replies
AEAsh Elixir
Created by harry on 1/28/2023 in #support
Cache & Query Time
Does AshPostgres cache queries?
5 replies
AEAsh Elixir
Created by harry on 1/22/2023 in #support
Multi-node deployments
How does ash work when deployed with multiple nodes e.g. across AWS regions are the notifiers only called on the node where it occurs or are they distributed?
3 replies
AEAsh Elixir
Created by harry on 1/21/2023 in #support
Sending emails with Ash
Does Ash have a wrapper for Sending emails or a recommended way to send transactional emails e.g. Confirm Account?
24 replies
AEAsh Elixir
Created by harry on 1/21/2023 in #support
Cannot use `^variable` outside of match clauses
This error disappears when you add import Ash.Query but then Elixir complains that it is an unneeded import, is there a better way around this?
6 replies
AEAsh Elixir
Created by harry on 1/21/2023 in #support
How to manage `belongs_to` relations
I have this action in my resource
create :new_token do
accept([:nickname, :scopes, :system, :expires_at])

argument :user, Project.Resources.Accounts.User do
allow_nil?(false)
end

change(manage_relationship(:user, on_match: :relate))

# ...
end
create :new_token do
accept([:nickname, :scopes, :system, :expires_at])

argument :user, Project.Resources.Accounts.User do
allow_nil?(false)
end

change(manage_relationship(:user, on_match: :relate))

# ...
end
but when I create a new token, the user and user_id are nil
|> Ash.Changeset.for_create(:new_token, %{
user: user,
system: true,
scopes: ["read", "write"],
expires_at:
DateTime.utc_now()
|> DateTime.add(@max_age, :second)
|> DateTime.truncate(:second)
})
|> Ash.Changeset.for_create(:new_token, %{
user: user,
system: true,
scopes: ["read", "write"],
expires_at:
DateTime.utc_now()
|> DateTime.add(@max_age, :second)
|> DateTime.truncate(:second)
})
11 replies
AEAsh Elixir
Created by harry on 1/21/2023 in #support
`AshPhoenix.Form` not applying changes
I am using forms as below:
def new(conn, _params) do
form = AshPhoenix.Form.for_create(Project.User, :new_account, api: Project.Accounts, as: "user")
render(conn, "new.html", changeset: form)
end

def create(conn, %{"user" => user_params} = _params) do
case AshPhoenix.Form.for_create(Project.User, :new_account, api: Project.Accounts, as: "user")
|> AshPhoenix.Form.submit(params: user_params) do
{:ok, user} ->
conn
|> put_flash(:info, "User created successfully.")
|> Project.UserAuth.log_in_user(user)

{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def new(conn, _params) do
form = AshPhoenix.Form.for_create(Project.User, :new_account, api: Project.Accounts, as: "user")
render(conn, "new.html", changeset: form)
end

def create(conn, %{"user" => user_params} = _params) do
case AshPhoenix.Form.for_create(Project.User, :new_account, api: Project.Accounts, as: "user")
|> AshPhoenix.Form.submit(params: user_params) do
{:ok, user} ->
conn
|> put_flash(:info, "User created successfully.")
|> Project.UserAuth.log_in_user(user)

{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
And the action :new_account is as follows:
create :new_account do
accept([:email, :password])

change(
{Project.Changes.MaybeHashSecret, changeset_key: :new_password, resource_key: :password}
)
end
create :new_account do
accept([:email, :password])

change(
{Project.Changes.MaybeHashSecret, changeset_key: :new_password, resource_key: :password}
)
end
I would expect on submit that my change is hashing the secret however it isn't and the change is running when the form is created. The change is also below
def change(changeset, opts, _) do
changeset_key = Keyword.get(opts, :changeset_key, @default_changeset_key)
resource_key = Keyword.get(opts, :resource_key, @default_resource_key)

# TODO get working, not hashing secrets
with secret <- Ash.Changeset.get_argument_or_attribute(changeset, changeset_key),
true <- is_binary(secret),
hash <- Argon2.hash_pwd_salt(secret) do
Ash.Changeset.force_change_attribute(changeset, resource_key, hash)
else
_ -> changeset
end
end
def change(changeset, opts, _) do
changeset_key = Keyword.get(opts, :changeset_key, @default_changeset_key)
resource_key = Keyword.get(opts, :resource_key, @default_resource_key)

# TODO get working, not hashing secrets
with secret <- Ash.Changeset.get_argument_or_attribute(changeset, changeset_key),
true <- is_binary(secret),
hash <- Argon2.hash_pwd_salt(secret) do
Ash.Changeset.force_change_attribute(changeset, resource_key, hash)
else
_ -> changeset
end
end
Any ideas?
27 replies