【发布时间】:2023-03-17 21:31:01
【问题描述】:
如何在包含的MyApp.MyUniversalModule 中获取调用者模块并通过 field_name 参数获得?
defmodule MyApp.MyUniversalModule do
def gen_and_check_unique(changeset, field_name) do
token = random_string()
# MyApp.MyQueryableModule |> Repo.get_by(field_name, token)
end
def random_string(length \\ 8) do
:crypto.strong_rand_bytes(length) |> Base.url_encode64 |> binary_part(0, length)
end
end
defmodule MyApp.MyQueryableModule do
use MyApp.Web, :model
import MyApp.MyUniversalModule
schema "items" do
field :name, :string
field :token, :string
end
def changeset(model, params) do
model
|> cast(params, ~w(name), ~w())
|> gen_and_check_unique(:token)
end
end
【问题讨论】:
标签: elixir phoenix-framework ecto