0
コンソールのレコードの値を更新しようとしていますies -S mix
。フェニックス:チェンジセットは外部キーの変更を考慮しません
iex> video = Repo.one(from v in Video, limit: 1)
%Rumbl.Video{...}
ビデオのタイトルを変更すると、すべて正常に動作しているようです。
iex> changeset = Video.changeset(video, %{title: "some title"})
#Ecto.Changeset<action: nil, changes: %{title: "some title"},
errors: [], data: #Rumbl.Video<>, valid?: true>
しかし、外部キーを変更するには効果がないようです:
iex> changeset = Video.changeset(video, %{category_id: 3})
#Ecto.Changeset<action: nil, changes: %{},
errors: [], data: #Rumbl.Video<>, valid?: true>
は私がaccoungに入れなければ外部キーの変更のために何をすべき?
ここモデル
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, foreign_key: :user_id
belongs_to :category, Rumbl.Category, foreign_key: :category_id
timestamps()
end
@required_fields ~w(url title description)
@optional_fields ~w(category_id)
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields, @optional_fields)
|> validate_required([:url, :title, :description])
|> assoc_constraint(:category)
end
末端エクト2.2、the fourth argument to cast
is optsで