【发布时间】:2016-12-30 23:55:00
【问题描述】:
我正在尝试使用 Repo.update 更新我的数据库中的现有记录:
def subscribe_email(conn, %{"email-address"=>email_address, "shop"=>shop}) do
current_record = Repo.all(%Oauth.EmailAddress{email_address: email_address, active: false, shop: shop, verified: :true})
current_record = Ecto.Changeset.change(current_record, active: :true)
case Repo.update current_record do
{:ok, struct} -> IO.puts "updated correctly."
{:error, changeset} -> IO.puts "did not update"
end
end
我有一个 %Oauth.EmailAddress 的模型:
defmodule Oauth.EmailAddress do
use Ecto.Model
schema "email_address" do
field :email_address, :string
field :active, :boolean
field :shop, :string
field :verified, :boolean
timestamps
end
end
当我点击 subscribe_email() 时,会引发异常:
** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for %Oauth.EmailAddress
我知道我需要从 Ecto.Queryable 实现 to_query()。但是,我不知道如何 这样做。我对 Elixir 中的 Protocols 不熟悉,虽然我看过官方文档,但我没有使用过 Protocols。请解释如何为我的结构实现 to_query()。
【问题讨论】:
标签: elixir phoenix-framework ecto