Is it possible to list and select the relationship in the Admin resource creation form?

For example: I'd like to list all registered users and select one to associate with the Pet I'm creating. Is there something ready to do this?
No description
11 Replies
schnady
schnady•3mo ago
you have pet belongs_to user and user has_many pets ?
JVMartyns
JVMartynsOP•3mo ago
Please ignore the fact that they are in different databases.
schnady
schnady•3mo ago
Pet create action in resource
actions do

create :create do
accept [:user_id, :age, :name]
end
actions do

create :create do
accept [:user_id, :age, :name]
end
in your domain
resource(MyApp.Ash.Pets) do
define(:create_pet, action: :create)
resource(MyApp.Ash.Pets) do
define(:create_pet, action: :create)
and
forms do
form :create_pet, args: [:user_id]
forms do
form :create_pet, args: [:user_id]
JVMartyns
JVMartynsOP•3mo ago
defmodule JoseValimIsMyHero.Accounts.User do
use Ash.Resource,
domain: JoseValimIsMyHero.Accounts,
data_layer: AshPostgres.DataLayer,
extensions: [AshGraphql.Resource, AshJsonApi.Resource, AshAdmin.Resource]

resource do
base_filter expr(is_nil(deleted_at))
end

postgres do
table "users"
repo JoseValimIsMyHero.Repo.Postgres
base_filter_sql "created_at IS NULL"
end

graphql do
type :user
end

json_api do
type "user"
end

actions do
defaults [:read]

read :list do
prepare(build(sort: :email))

pagination do
required?(false)
offset?(true)
keyset?(true)
countable(true)
end
end

create :create do
accept [:name, :email, :password]
end

update :update do
accept [:name, :email, :password]
end

destroy :destroy do
change set_attribute(:deleted_at, DateTime.utc_now())
end
end

attributes do
uuid_primary_key :id
attribute :name, :string, public?: true

attribute :email, :string do
allow_nil? false
public? true

constraints match: ~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/
end

attribute :password, :string do
allow_nil? false
sensitive? true

constraints min_length: 8, max_length: 100
end

attribute :deleted_at, :datetime, allow_nil?: true
create_timestamp :created_at, public?: true
update_timestamp :updated_at, public?: true
end

identities do
# Allow only one user with the same email
identity :email, [:email]
end

relationships do
has_many :pets, JoseValimIsMyHero.Animals.Pet
end
end
defmodule JoseValimIsMyHero.Accounts.User do
use Ash.Resource,
domain: JoseValimIsMyHero.Accounts,
data_layer: AshPostgres.DataLayer,
extensions: [AshGraphql.Resource, AshJsonApi.Resource, AshAdmin.Resource]

resource do
base_filter expr(is_nil(deleted_at))
end

postgres do
table "users"
repo JoseValimIsMyHero.Repo.Postgres
base_filter_sql "created_at IS NULL"
end

graphql do
type :user
end

json_api do
type "user"
end

actions do
defaults [:read]

read :list do
prepare(build(sort: :email))

pagination do
required?(false)
offset?(true)
keyset?(true)
countable(true)
end
end

create :create do
accept [:name, :email, :password]
end

update :update do
accept [:name, :email, :password]
end

destroy :destroy do
change set_attribute(:deleted_at, DateTime.utc_now())
end
end

attributes do
uuid_primary_key :id
attribute :name, :string, public?: true

attribute :email, :string do
allow_nil? false
public? true

constraints match: ~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/
end

attribute :password, :string do
allow_nil? false
sensitive? true

constraints min_length: 8, max_length: 100
end

attribute :deleted_at, :datetime, allow_nil?: true
create_timestamp :created_at, public?: true
update_timestamp :updated_at, public?: true
end

identities do
# Allow only one user with the same email
identity :email, [:email]
end

relationships do
has_many :pets, JoseValimIsMyHero.Animals.Pet
end
end
defmodule JoseValimIsMyHero.Animals.Pet do
use Ash.Resource,
domain: JoseValimIsMyHero.Animals,
data_layer: AshSqlite.DataLayer,
extensions: [AshGraphql.Resource, AshJsonApi.Resource, AshAdmin.Resource]

sqlite do
table "pets"
repo JoseValimIsMyHero.Repo.Sqlite
end

graphql do
type :pet
end

json_api do
type "pet"
end

actions do
defaults [:read, :destroy]

create :create do
accept [:name, :age, :user_id]
end

update :update do
accept [:name, :age, :user_id]
end
end

attributes do
uuid_primary_key :id
attribute :name, :string, allow_nil?: false

attribute :age, :integer do
constraints min: 0
end
end

relationships do
belongs_to :user, JoseValimIsMyHero.Accounts.User
end
end
defmodule JoseValimIsMyHero.Animals.Pet do
use Ash.Resource,
domain: JoseValimIsMyHero.Animals,
data_layer: AshSqlite.DataLayer,
extensions: [AshGraphql.Resource, AshJsonApi.Resource, AshAdmin.Resource]

sqlite do
table "pets"
repo JoseValimIsMyHero.Repo.Sqlite
end

graphql do
type :pet
end

json_api do
type "pet"
end

actions do
defaults [:read, :destroy]

create :create do
accept [:name, :age, :user_id]
end

update :update do
accept [:name, :age, :user_id]
end
end

attributes do
uuid_primary_key :id
attribute :name, :string, allow_nil?: false

attribute :age, :integer do
constraints min: 0
end
end

relationships do
belongs_to :user, JoseValimIsMyHero.Accounts.User
end
end
schnady
schnady•3mo ago
see this you can have the form (for pet creation) have an argument for user than
form =
MyApp.Ash.Pet.form_to_create_pet( .... .user.id )
form =
MyApp.Ash.Pet.form_to_create_pet( .... .user.id )
ZachDaniel
ZachDaniel•3mo ago
IIRC if you set this on the target resource you can get multi-select/single select: https://hexdocs.pm/ash_admin/dsl-ashadmin-resource.html#admin-label_field
JVMartyns
JVMartynsOP•3mo ago
It's works. Thank you very much.
No description
No description
JVMartyns
JVMartynsOP•3mo ago
Is this a bug? 🤔 It turns back into a text field if you disablelabel_field
admin do
actor? true
label_field :email
end
admin do
actor? true
label_field :email
end
No description
No description
ZachDaniel
ZachDaniel•3mo ago
Looks like a bug to me, yes Please open an issue Ideally if you can, reproduce that with the resources in the /dev app In the ash_admin repo You can run that with iex -S mix dev
JVMartyns
JVMartynsOP•3mo ago
Okay, I'll try! I chose to post it here in this thread because it's already related to the initial topic.
JVMartyns
JVMartynsOP•3mo ago
GitHub
Authentication fields render as select inputs when label_field is s...
Code of Conduct I agree to follow this project's Code of Conduct AI Policy I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue. Versions Wh...

Did you find this page helpful?