How can I retrieve attributes from a resource record

Which is the preferred way to extract attributes from an Ash record?
record = App.Model |> Ash.Query.for_read(:last) |> App.read_one!

# how to get attributes from it?
record.attributes ????
record = App.Model |> Ash.Query.for_read(:last) |> App.read_one!

# how to get attributes from it?
record.attributes ????
Must it be done through Ash.Resource.Info or through Ash.Changeset or it should not be done alltogether 🙂 The use case is that I create a record from an external api call and in the same flow I must send a sync to another external api. I must use the record attributes instead of the received attributes because sometimes attributes coming from the first api are missing. I need something like Ash.Changeset data or attributes but for the record.
1 Reply
ZachDaniel
ZachDaniel•2y ago
The attributes will be on the record by name i.e if you had an attribute :first_name you'd say record.first_name If you want to get all attribute values, you could do something like this:
resource
|> Ash.Resource.Info.attributes()
|> Map.new(fn attribute ->
{name, Map.get(record, attribute.name)}
end)
resource
|> Ash.Resource.Info.attributes()
|> Map.new(fn attribute ->
{name, Map.get(record, attribute.name)}
end)

Did you find this page helpful?