Reading private attributes stopped working
I recently started not getting back private attributes in my
update
action. Record looks good via Resource.read
, but attribute is nil
. Perhaps due to a recent regression or change in semantics?24 Replies
🤔 interesting. So on an update the result doesn't contain private attributes, but when you read it back they are there?
Are the attribute values
nil
on the way in?I've just noticed this, and the action I was working on is a
manual
, getting the attributes with Changeset.get_attribute
. I'm on ash
trunk and just updated.
Not nil
in Resource.read
, looks good in Postgres.🤔 oh, so you're talking about before the update happens
i.e
Yep
So generally speaking, that is caused by you passing in records to an update action that don't have all of the attributes selected
I'd like to add an elegant "reselection" process to update actions that allow you to express the data that you need and have it be reselected before the action
but until then, you'll have to make sure you've selected everything on a record before providing it for an update, or do the reselection yourself
TO confirm that its
nil
because it hasn't been selected:
Ok, right on. Something along the lines of:
You could write a generic tool for this:
Great! Thanks a bunch.
Hmm, it doesn't seem to be working for the action in question.
(still getting
nil
)Can you confirm if this:
Ash.Resource.selected?(changeset.data, :attr) #<- will tell you if the attr was selected
is returning true/false where you expect it to?
or show me where you are doing get_attribute
for exampleAsh.Resource.selected?(cs.data, :attr) == true
WeirdlyAnd what does
changeset.attributes
have in it?
Does it contain %{attr: nil}
?Nope, it is
%{}
:thinkies:
what is
cs.data.attr
It is
KeyError
😁what?
is
:attr
not an attribute of your resource?
To be clear, when I say :attr
I mean whatever attribute you're checking withIn that case, it is
nil
.So the passed in record has a
nil
value for the attribute, and it was selected, meaning that it should be nil
Why should it not be nil
?Righto, the passed in record does have it as
nil
, I was thinking we were trying to indicate that it should be reselected.
(the DB record is not nil
)gotcha. Well you could remove that
if Enum.all
check on your reselect change
or add an option in there to force reselect some of themRight on, then it would simply indicate to reload the entire record, which is just fine 🙂
You could also use changeset context for that
and then
where you call the action
if you want to make it opt-in
I think it's probably acceptable to have it, but it seems more idiomatic to ensure that the callers are threading it through rather than pushing this into the actions themselves.
Actually, a change to do a reload if the indicated attributes are
nil
would probably be a fine middleground.
I think you'll want
Map.get
there but yeahYep, works great, thanks very much mate! 🙂