完成示例指南。本章中的详细内容在我的应用程序中不起作用。看起来非常简单的东西。我有一个视频模型:
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会话中,我按如下方式加载视频记录:
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}}
我不明白为什么变更集没有任何变化。这是指南指导进行关联的方式。我错过了什么吗?
谢谢, 约翰
答案 0 :(得分:4)
我明白了。我需要将category_id添加到视频模型中的参数列表中:
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:url, :title, :description, :category_id])
|> validate_required([:url, :title])
|> assoc_constraint(:category)
end