Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ecto.Repo receives a struct that does not implement Access behaviour

i have a problem with an Ecto Repo and a schema in one of my tests. The schema is the following:

defmodule Elixirserver.Transactions.Bank do
  @behaviour Elixirserver.ContentDump

  use Ecto.Schema
  import Ecto.Changeset
  alias Elixirserver.Transactions.Account

  @attrs [:name, :code]

  schema "banks" do
    field(:name, :string)
    field(:code, :string)
    has_many(:account, Account)
    timestamps()
  end

  @doc false
  def changeset(bank, attrs \\ []) do
    bank
    |> cast(attrs, @attrs)
    |> validate_required(@attrs)
  end

  def to_json(bank) do
    %{
      id: bank.id,
      name: bank.name,
      code: bank.code,
      type: "BANK"
    }
  end
end

When i try to execute a test i obtain the following:

(UndefinedFunctionError) function 
Elixirserver.Transactions.Bank.fetch/2 is undefined 
(Elixirserver.Transactions.Bank does not implement the Access behaviour)

The test is this:

def create(conn, %{"bank" => bank_params}) do
  with {:ok, %Bank{} = bank} <- Transactions.create_bank(bank_params) do
    conn
    |> put_status(:created)
    |> put_resp_header("location", bank_path(conn, :show, bank))
    |> render("show.json", id: bank["id"])
  end
end

Now, apparently this is because the Access behaviour is not implemented. Do i have to provide it explicitly ?

I am using ExMachina to generate fixtures, and i generated the resources with mix phx.gen.json.

like image 221
Bruno Ripa Avatar asked Jun 18 '18 20:06

Bruno Ripa


1 Answers

bank["id"] is most probably the problem. Structs don't implement the access interface, you should use the dot so this should work: bank.id.

Details can be found here.

like image 102
NoDisplayName Avatar answered Nov 03 '22 12:11

NoDisplayName