Here is the code that got it working:
# libvisionoardsoard.ex
defmodule Vision.Boards.Board do
use Ecto.Schema
import Ecto.Changeset
schema "boards" do
field :owner, :string
field :team_name, :string
field :title, :string
has_many :members, Vision.Members.Member
timestamps()
end
@doc false
def changeset(board, attrs) do
board
|> cast(attrs, [:title, :owner, :team_name])
|> validate_required([:title, :owner, :team_name])
end
end
# libvisionmembersmember.ex
defmodule Vision.Members.Member do
use Ecto.Schema
import Ecto.Changeset
schema "members" do
field :role, :string
field :username, :string
belongs_to :board, Vision.Boards.Board
timestamps()
end
@doc false
def changeset(member, attrs) do
member
|> cast(attrs, [:username, :role])
|> validate_required([:username, :role])
end
end
And then in a migration:
# priv
epomigrations*_member_belongs_to_board.exs
defmodule Vision.Repo.Migrations.MemberBelongsToBoard do
use Ecto.Migration
def change do
alter table(:members) do
add :board_id, references (:boards)
end
end
end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…