如何在我的模型文件中使用 Repo 模块
How to use Repo module in my model file
在我的标签模型代码
schema "tags" do
field :name, :string
field :parent, :integer # parent tag id
timestamps
end
def add_error_when_not_exists_tag_id(changeset, params) do
tags = Repo.all(Tag)
is_exists_tag_id = Enum.reduce(tags, fn(x, acc) -> acc || (x.id === params.parent) end)
if is_exists_tag_id, do: changeset, else: add_error(changeset, :parent, "not exists parent!")
end
以上代码导致以下错误。
(UndefinedFunctionError) undefined function: Repo.all/1 (module Repo is not available)
我可以修复错误吗?
标签模型是嵌套标签模型。
标签可以有父标签。
最后的代码如下。这很好用。
模型中
def add_error_when_not_exists_tag_id(changeset, params, tags) do
is_exists_tag_id = Enum.reduce(tags, false, fn(x, acc) -> acc || (Integer.to_string(x.id) === params["parent"]) end)
if is_exists_tag_id, do: changeset, else: add_error(changeset, :parent, "The tag is not exists.")
end
在控制器中
def create(conn, %{"tag" => tag_params}) do
changeset = Tag.changeset(%Tag{}, tag_params)
|> Tag.add_error_when_not_exists_tag_id(tag_params, Repo.all(Tag))
//
// ...
您不能使用 Repo
变量,因为它在此模块中不可用。您需要为其设置别名:
alias MyApp.Repo
在控制器中,这是在 web.ex
中为您处理的,它在您的模块中被调用:
use MyApp.Web, :controller
但是,我强烈建议您避免在模型中使用Repo
。您的模型应该是纯净的,这意味着它们不应该有副作用。在您的模型中调用函数对于特定输入(幂等性)应始终具有相同的输出。
在此示例中,您可以将函数的实现更改为:
def add_error_when_not_exists_tag_id(changeset, params, tags) do
is_exists_tag_id = Enum.reduce(tags, fn(x, acc) -> acc || (x.id === params.parent) end)
if is_exists_tag_id, do: changeset, else: add_error(changeset, :parent, "not exists parent!")
end
您可以在控制器中调用 Repo.all
并将标签传递给函数。
如果您有更复杂的行为,请考虑创建一个使用该函数的 TagService
模块并调用 Repo.all
在我的标签模型代码
schema "tags" do
field :name, :string
field :parent, :integer # parent tag id
timestamps
end
def add_error_when_not_exists_tag_id(changeset, params) do
tags = Repo.all(Tag)
is_exists_tag_id = Enum.reduce(tags, fn(x, acc) -> acc || (x.id === params.parent) end)
if is_exists_tag_id, do: changeset, else: add_error(changeset, :parent, "not exists parent!")
end
以上代码导致以下错误。
(UndefinedFunctionError) undefined function: Repo.all/1 (module Repo is not available)
我可以修复错误吗?
标签模型是嵌套标签模型。
标签可以有父标签。
最后的代码如下。这很好用。
模型中
def add_error_when_not_exists_tag_id(changeset, params, tags) do
is_exists_tag_id = Enum.reduce(tags, false, fn(x, acc) -> acc || (Integer.to_string(x.id) === params["parent"]) end)
if is_exists_tag_id, do: changeset, else: add_error(changeset, :parent, "The tag is not exists.")
end
在控制器中
def create(conn, %{"tag" => tag_params}) do
changeset = Tag.changeset(%Tag{}, tag_params)
|> Tag.add_error_when_not_exists_tag_id(tag_params, Repo.all(Tag))
//
// ...
您不能使用 Repo
变量,因为它在此模块中不可用。您需要为其设置别名:
alias MyApp.Repo
在控制器中,这是在 web.ex
中为您处理的,它在您的模块中被调用:
use MyApp.Web, :controller
但是,我强烈建议您避免在模型中使用Repo
。您的模型应该是纯净的,这意味着它们不应该有副作用。在您的模型中调用函数对于特定输入(幂等性)应始终具有相同的输出。
在此示例中,您可以将函数的实现更改为:
def add_error_when_not_exists_tag_id(changeset, params, tags) do
is_exists_tag_id = Enum.reduce(tags, fn(x, acc) -> acc || (x.id === params.parent) end)
if is_exists_tag_id, do: changeset, else: add_error(changeset, :parent, "not exists parent!")
end
您可以在控制器中调用 Repo.all
并将标签传递给函数。
如果您有更复杂的行为,请考虑创建一个使用该函数的 TagService
模块并调用 Repo.all