我想根据另一个字段的值设置结构的字段值。
这是我到达的方法,我等到调用changeset
并将其插入验证程序之间。这是做这样事情的正确/预期方法吗?
这样做的一个问题是,如果有人写fruit = %Fruit(%{name: :lemon})
,他们将无法访问fruit.tastes
,直到他们保存并加载记录(或者至少调用changeset
周围的包装器)和apply_changeset
)。不过,这并不是什么大问题,只是一个不错的选择。我不仅仅使用Fruit.tastes(fruit)
函数的原因是,口味必须在数据库中可索引。
defmodule Food.Fruit do
use Ecto.Schema
import Ecto.Changeset
@required_attributes [:name]
@optional_attributes []
@valid_names [:lemon, :banana]
@valid_tastes [:sour, :sweet]
schema "fruits" do
field :name, :string #user specified
field :tastes, :string #inferred from name
timestamps()
end
def changeset(fruit, attrs) do
fruit
|> cast(attrs, @required_attribtues ++ @optional_attributes)
|> validate_required(@required_attributes)
|> validate_inclusion(:name, @valid_names)
|> infer_colour
# these are debately needed, we should set things correctly in infer_colour
# but perhaps doesn't hurt to check to ward against future stupid.
|> validate_required(:tastes)
|> validate_inclusion(:tastes, @valid_tastes)
end
# name is invalid, so we can't infer any value
def infer_colour(%Ecto.Changeset{errors: [{:name, _} | _]} = changeset), do: changeset
# name isn't error'd so we can infer a value
def infer_colour(%Ecto.Changeset{} = changeset) do
case fetch_field(changeset, :name) do
# match on {:data, value} or {:changes, value} so if the user passes a
# custom :tastes to Fruit.changeset we default to overwriting it
# with our correct value (though cast(...) should be stripping :tastes if present)
{_, :lemon} -> change(changeset, %{tastes: :sour})
{_, :apple} -> change(changeset, %{tastes: :sweet})
{_. name} -> add_error(changeset, :tastes, "unknown :tastes for #{name}")
:error -> add_error(changeset, :tastes, "could not infer :tastes, no :name found")
end
end
end
答案 0 :(得分:0)
我认为您的解决方案没有任何问题,但是您是否考虑过在投射前在attrs中添加:taste,例如:
defmodule Food.Fruit do
use Ecto.Schema
import Ecto.Changeset
@required_attributes [:name]
@optional_attributes []
@valid_names [:lemon, :banana]
@valid_tastes [:sour, :sweet]
schema "fruits" do
field :name, :string #user specified
field :tastes, :string #inferred from name
timestamps()
end
def changeset(fruit, attrs) do
attrs = resolve_taste(attrs)
fruit
|> cast(attrs, @required_attribtues ++ @optional_attributes)
|> validate_required(@required_attributes)
|> validate_inclusion(:name, @valid_names)
|> validate_required(:tastes)
|> validate_inclusion(:tastes, @valid_tastes)
end
def resolve_taste(%{name: :lemon}=a),
do: Map.put_new(a, :tastes, :sour)
def resolve_taste(%{name: :apple}=a),
do: Map.put_new(a, :tastes, :sweet)
def resolve_taste(any), do: any
end
仍然应该正确验证,并且您不必担心口味是否发生变化。