Reputation: 8189
I want to update some meta data that is not a permitted
attribute in my schema's changeset:
def changeset(%Comment{} = comment, attrs) do
comment
|> cast(attrs, [:text])
|> validate_required([:text])
end
And then something like:
changeset = Comment.changeset(commet, %{under_moderation: true})
Repo.update(changeset)
Since under_moderation
is not whitelisted, it gets ignored. What options do I have to force the update? If there are multiple options, is there a convention?
Upvotes: 0
Views: 52
Reputation: 1445
I would just create another changeset function that has the rights to set the value.
def admin_changeset(%Comment{} = comment, attrs) do
comment
|> cast(attrs, [:text, :under_moderation])
|> validate_required([:text])
end
Then simply use that to update the value. As you can see, I named it admin_changeset
because it seems like this is a value that would be set by an admin. In your controller or context module, simply check the user role (if you have something like that) and then decide which changeset function you want to use.
Upvotes: 1