4
我正在關注Chris McCord的「Programming Phoenix」一書,在第6章中,在User
和Video
之間建立了關係。模型之間缺少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
我怎樣才能優雅地處理這種情況的內容?
該評論聽起來不正確。你可以發佈'web/models/user.ex'的內容嗎?可能缺少'has_many:視頻,...'。 – Dogbert
當然,只是更新了答案。 – Henrique