phoenix framework - How to implement to_query(data) in Elixir Struct -
i attempting update existing records in database using repo.update:
def subscribe_email(conn, %{"email-address"=>email_address, "shop"=>shop}) 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 {:ok, struct} -> io.puts "updated correctly." {:error, changeset} -> io.puts "did not update" end end
i have model %oauth.emailaddress:
defmodule oauth.emailaddress use ecto.model schema "email_address" field :email_address, :string field :active, :boolean field :shop, :string field :verified, :boolean timestamps end end
when hit subscribe_email(), exception raised:
** (protocol.undefinederror) protocol ecto.queryable not implemented %oauth.emailaddress
i know need implement to_query() ecto.queryable. however, not know how this. not familiar protocols in elixir, although have read official documentation, have not used protocols. please explain how implement to_query() struct.
that error bit misleading. repo.all
doesn't accept struct fields filled in that. you're looking this:
current_record = from(oauth.emailaddress) |> where(email_address: email_address, active: false, shop: shop, verified: :true) |> repo.all
also, since you're passing result ecto.changeset.change
, want 1 record, not list of records:
current_record = from(oauth.emailaddress) |> where(email_address: email_address, active: false, shop: shop, verified: :true) |> repo.one
note fail if there's more 1 matching record query.
Comments
Post a Comment