2016-08-04 23 views
4

私はChris McCordの "Programming Phoenix"の本を読んでおり、第6章ではUserVideoの間に関係が作成されています。モデル間のectoの関連付けがありません

mix phoenix.serverでそれを実行しようとすると、次のエラーが現れた場合:

書籍の正誤表の上に行く
Request: GET /manage/videos 
** (exit) an exception was raised: 
    ** (ArgumentError) schema Rumbl.User does not have association :videos 
     (ecto) lib/ecto/association.ex:121: Ecto.Association.association_from_schema!/2 

、ログインしているユーザーがいずれかを持っていないため、この問題が発生したことを言及し、他のユーザーからのコメントがありますそれらに関連付けられたビデオ。

以下

は、どのように私は優雅にこのケースを扱うことができるuser.ex

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

    schema "users" do 
     field :name, :string 
     field :username, :string 
     field :password, :string, virtual: true 
     field :password_hash, :string 

     timestamps 
    end 

    def changeset(user, params \\ :empty) do 
     user 
     |> cast(params, ~w(name username), []) 
     |> validate_length(:username, min: 1, max: 20) 
    end 

    def registration_changeset(user, params) do 
     user 
     |> changeset(params) 
     |> cast(params, ~w(password), []) 
     |> validate_length(:password, min: 6, max: 100) 
     |> put_pass_hash() 
    end 

    def put_pass_hash(changeset) do 
     case changeset do 
      %Ecto.Changeset{valid?: true, changes: %{password: pass}} -> 
       put_change(changeset, :password_hash, Comeonin.Bcrypt.hashpwsalt(pass)) 
       _-> changeset 
     end 
    end 
end 

の内容でしょうか?

+0

コメントは間違っています。 'web/models/user.ex'の内容を投稿してください。おそらく 'has_many:videos、... 'がありません。 – Dogbert

+0

確かに、答えを更新しました。 – Henrique

答えて

5

あなたはweb/models/user.exschema "users"has_many :videos, Rumbl.Videoを追加するのを忘れ:第6章で述べたように(P1_0のPDFのページ100)

schema "users" do 
    # ... 
    has_many :videos, Rumbl.Video 
    # ... 
end 

をしてthis snippetインチ

関連する問題