2017-11-19 13 views
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

答えて

1

なく、オプションのフィールドです。それ以前のオプションフィールドはv2.1では推奨されず、代わりにvalidate_requiredを使用することを推奨していました。これは私がchangelogで見つけることができませんでしたがapparently removed in v2.2.0でした。あなたはエクト2.2のために、このようにコードを変更する必要があります。

struct 
|> cast(params, @required_fields ++ @optional_fields) 
|> validate_required([:url, :title, :description]) 

または次の操作を行います。

@required_fields ~w(url title description)a 
@optional_fields ~w(category_id)a 

|> cast(params, @required_fields ++ @optional_fields) 
|> validate_required(@required_fields) 
関連する問題