miguels
miguels
AEAsh Elixir
Created by miguels on 9/27/2023 in #support
Persist embedded resources as :text instead of :map
In my app I'm using Cocktail (https://hexdocs.pm/cocktail) to manage scheduled events which I'd like to save to a text field using Cocktail.Schedule.to_i_calendar/1. What I have so far: I use an embedded schema to generate the API used in LV forms and GraphQL
defmodule Schedule do
use Ash.Resource,
data_layer: :embedded,
extensions: [AshGraphql.Resource]

graphql do
type :schedule
end

attributes do
attribute :count, :integer
// ...
end

validations do
validate numericality(:count, greater_than: 0)
end
end
defmodule Schedule do
use Ash.Resource,
data_layer: :embedded,
extensions: [AshGraphql.Resource]

graphql do
type :schedule
end

attributes do
attribute :count, :integer
// ...
end

validations do
validate numericality(:count, greater_than: 0)
end
end
Which I use in another resource
attributes do
integer_primary_key :id

// ...

attribute :schedule, Schedule

// ...
end
attributes do
integer_primary_key :id

// ...

attribute :schedule, Schedule

// ...
end
And then realized that embedded schemas are persisted as maps and I haven't found a way to change this behaviour
def up do
alter table(:table) do
add :schedule, :map
end
end
def up do
alter table(:table) do
add :schedule, :map
end
end
I've also briefly tried setting up a custom type instead without much success.
13 replies
AEAsh Elixir
Created by miguels on 5/3/2023 in #support
Users getting logged out every few hours
I recently switched our auth from phx.gen.auth to AshAuthentication. The switch was very smooth and users didn't notice anything except for one detail: they're now getting logged out every few hours (exact time seems to vary). I've been going through all documentation and the Discord but haven't found anything related. The only thing I found was this option token_lifetime, changing the value hasn't helped so far, though. Any ideas what could be happening or were I can start debugging?
8 replies
AEAsh Elixir
Created by miguels on 4/23/2023 in #support
How to create has_one relationships in a nested form
I have a resource called Transaction that has a has_one relationship with a resource called RecurrencePattern. When creating a new transaction (usually through a form in LV) I want to also create a matching recurrence_pattern. This works great when using the create action directly, but I'm getting an error when using an AshPhoenix.Form (I believe I got it to work at some point but don't know how to get back there again 😅 ) This works:
Transaction.create!(%{
amount: 100,
description: "Test transaction",
date: Date.utc_today(),
category_id: context.category.id,
currency_id: context.currency.id,
method_id: context.method.id,
rate_id: context.rate.id,
type_id: context.type.id,
end_date: Timex.shift(Date.utc_today(), months: 1),
recurrence_pattern: %{
recurrence_type_id: context.recurrence_type.id,
count: 5
}
})
Transaction.create!(%{
amount: 100,
description: "Test transaction",
date: Date.utc_today(),
category_id: context.category.id,
currency_id: context.currency.id,
method_id: context.method.id,
rate_id: context.rate.id,
type_id: context.type.id,
end_date: Timex.shift(Date.utc_today(), months: 1),
recurrence_pattern: %{
recurrence_type_id: context.recurrence_type.id,
count: 5
}
})
This gives me an error:
params = %{
amount: 100,
description: "Test transaction",
date: Date.utc_today(),
category_id: context.category.id,
currency_id: context.currency.id,
method_id: context.method.id,
rate_id: context.rate.id,
type_id: context.type.id,
end_date: Timex.shift(Date.utc_today(), months: 1),
recurrence_pattern: %{
recurrence_type_id: context.recurrence_type.id,
count: 5
}
}

transaction =
AshPhoenix.Form.for_create(Transaction, :create,
api: Transactions,
forms: [auto?: true]
)
|> AshPhoenix.Form.add_form(:recurrence_pattern)
|> AshPhoenix.Form.validate(params)
|> AshPhoenix.Form.submit!()
params = %{
amount: 100,
description: "Test transaction",
date: Date.utc_today(),
category_id: context.category.id,
currency_id: context.currency.id,
method_id: context.method.id,
rate_id: context.rate.id,
type_id: context.type.id,
end_date: Timex.shift(Date.utc_today(), months: 1),
recurrence_pattern: %{
recurrence_type_id: context.recurrence_type.id,
count: 5
}
}

transaction =
AshPhoenix.Form.for_create(Transaction, :create,
api: Transactions,
forms: [auto?: true]
)
|> AshPhoenix.Form.add_form(:recurrence_pattern)
|> AshPhoenix.Form.validate(params)
|> AshPhoenix.Form.submit!()
The error inside the changeset:
%Ash.Error.Changes.Required{
field: :transaction_id,
type: :attribute,
resource: RecurrencePattern,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
%Ash.Error.Changes.Required{
field: :transaction_id,
type: :attribute,
resource: RecurrencePattern,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
42 replies