gpt4 book ai didi

phoenix-framework - Phoenix/Ecto - 关联不起作用

转载 作者:行者123 更新时间:2023-12-02 04:01:51 27 4
gpt4 key购买 nike

完成示例指南。本章中详细介绍的内容在我的应用程序中不起作用。看起来很简单的事情。我有一个视频模型:

defmodule Rumbl.Video do
use Rumbl.Web, :model

schema "videos" do
field :url, :string
field :title, :string
field :description, :string
belongs_to :user, Rumbl.User
belongs_to :category, Rumbl.Category

timestamps()
end

@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:url, :title, :description])
|> validate_required([:url, :title])
|> assoc_constraint(:category)
end
end

我还有一个类别模型:

defmodule Rumbl.Category do
use Rumbl.Web, :model

schema "categories" do
field :name, :string

timestamps()
end

@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name])
|> validate_required([:name])
end

def alphabetical(query) do
from c in query, order_by: c.name
end

def names_and_ids(query) do
from c in query, select: {c.name, c.id}
end
end

在 IEX session 中,我按如下方式加载视频记录:

iex(21)> video = Repo.one(from v in Video, limit: 1)
[debug] QUERY OK source="videos" db=16.0ms
SELECT v0."id", v0."url", v0."title", v0."description", v0."user_id", v0."category_id", v0."inserted_at", v0."updated_at" FROM "videos" AS v0 LIMIT 1 []
%Rumbl.Video{__meta__: #Ecto.Schema.Metadata<:loaded, "videos">,
category: #Ecto.Association.NotLoaded<association :category is not loaded>,
category_id: nil, description: "test1", id: 2,
inserted_at: #Ecto.DateTime<2017-01-02 06:50:26>, title: "test1",
updated_at: #Ecto.DateTime<2017-01-02 06:50:26>, url: "test1 video.com",
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 10}

我明白为什么类别和用户关联未加载。我没有预加载用户,也没有要加载的类别关联。

无论哪种方式,我的视频都已保存在内存中:

iex(22)> v.id
2

现在我加载我的类别:

iex(23)> category = Repo.get_by Category, name: "Comedy"
[debug] QUERY OK source="categories" db=0.0ms
SELECT c0."id", c0."name", c0."inserted_at", c0."updated_at" FROM "categories" AS c0 WHERE (c0."name" = $1) ["Comedy"]
%Rumbl.Category{__meta__: #Ecto.Schema.Metadata<:loaded, "categories">, id: 4,
inserted_at: #Ecto.DateTime<2017-01-07 07:03:00>, name: "Comedy",
updated_at: #Ecto.DateTime<2017-01-07 07:03:00>}

只是为了证明我拥有它:

iex(24)> category.id
4

现在我尝试将视频与类别关联起来:

iex(25)> changeset = Video.changeset(video, %{category_id: category.id})
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #Rumbl.Video<>,
valid?: true>
iex(26)> Repo.update(changeset)
{:ok,
%Rumbl.Video{__meta__: #Ecto.Schema.Metadata<:loaded, "videos">,
category: #Ecto.Association.NotLoaded<association :category is not loaded>,
category_id: nil, description: "test1", id: 2,
inserted_at: #Ecto.DateTime<2017-01-02 06:50:26>, title: "test1",
updated_at: #Ecto.DateTime<2017-01-02 06:50:26>, url: "test1 video.com",
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 10}}

我不明白为什么变更集中没有任何变化。这就是指南指示如何进行关联的方式。我错过了什么吗?

谢谢,约翰

最佳答案

我明白了。我需要将 Category_id 添加到视频模型中的参数列表中:

def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:url, :title, :description, :category_id])
|> validate_required([:url, :title])
|> assoc_constraint(:category)
end

关于phoenix-framework - Phoenix/Ecto - 关联不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com