Access 中没有匹配的函数子句。get/3 - 为什么会出现此错误?

no function clause matching in Access.get/3 - Why am I getting this error?

当我尝试从数据库中获取列表时出现此错误,我不明白为什么。

这是抛出错误的函数:

  def list_lectures do
    Lecture
    |> Repo.all()
    |> Repo.preload(:author [user: :credential])
  end

这是我在 运行 代码时得到的错误:

iex(1)> Ram.CMS.list_lectures
[debug] QUERY OK source="lectures" db=3.4ms decode=0.7ms queue=0.4ms
SELECT l0."id", l0."source", l0."theme", l0."time", l0."title", l0."author_id", l0."inserted_at", l0."updated_at" FROM "lectures" AS l0 [] 
** (FunctionClauseError) no function clause matching in Access.get/3    

    The following arguments were given to Access.get/3:

        # 1
        :author

        # 2
        [user: :credential]

        # 3
        nil

    Attempted function clauses (showing 5 out of 5):

        def get(%module{} = container, key, default)
        def get(map, key, default) when is_map(map)
        def get(list, key, default) when is_list(list) and is_atom(key)
        def get(list, key, _default) when is_list(list)
        def get(nil, _key, default)

    (elixir) lib/access.ex:265: Access.get/3
    (ram) lib/ram/cms.ex:24: Ram.CMS.list_lectures/0
iex(1)> 
  schema "lectures" do
    field :source, :string
    field :theme, :string
    field :time, :string
    field :title, :string
    belongs_to :author, Author

    timestamps()
  end
  schema "credentials" do
    field :email, :string
    field :password, :string
    belongs_to :user, User

    timestamps()
  end
schema "users" do
    field :name, :string
    field :role, :string
    has_one :credential, Credential

    timestamps()
  end

这是项目link:https://github.com/DavidNeumark/ram

Access.get/3 文档说:

Gets the value for the given key in a container (a map, keyword list, or struct that implements the Access behaviour).

你给 Access 的第一个参数。get/3:

:作者不是地图、关键字列表或结构。是原子

我猜你想预加载嵌套关联,所以它应该是这样的:

  def list_lectures do
    Lecture
    |> Repo.all()
    |> Repo.preload(author: [user: :credential])
  end

更新 :检查您的 github 后,lib/ram/cms/lecture.ex 遗漏了作者的别名。它导致上面的代码不起作用

defmodule Ram.CMS.Lecture do
  use Ecto.Schema
  import Ecto.Changeset

  alias Ram.CMS.Author

  schema "lectures" do
    field :source, :string
    field :theme, :string
    field :time, :string
    field :title, :string
    belongs_to :author, Author

    timestamps()
  end

  @doc false
  def changeset(lecture, attrs) do
    lecture
    |> cast(attrs, [:title, :time, :source, :theme])
    |> validate_required([:title, :time, :source, :theme])
  end
end