schema "users" do
field :name, :string
field :email, :string
field :active, :boolean, default: true
has_many :products, TestExAdmin.Product
has_many :noids, TestExAdmin.Noid
many_to_many :roles, TestExAdmin.Role, join_through: TestExAdmin.UserRole
end <div class="form-group">
<label class="col-sm-2 control-label" for="user_roles">Roles</label>
<div class="col-sm-10">
<input name="user[roles][]" type="hidden" value="">
<div class="checkbox"><label><input type="checkbox" name="user[roles][1]">role1</label></div>
</div>
</div> def changeset(model, params \\ %{}) do
model
|> cast(params, @required_fields, @optional_fields)
|> cast_assoc(:noids, required: false)
|> cast_assoc(:products, required: false)
|> cast_assoc(:roles, required: false)
end %{email: "te...@example.com", name: "Cory",
products: %{"1481120458618": %{_destroy: "0", price: "13.00",
title: "A product title"}}, roles: %{"1": "on"}} errors: [roles: {"is invalid", [type: {:array, :map}]Here is the SO question: http://stackoverflow.com/posts/41019905/edit
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/35335922-f158-4792-b4e9-d954204b9d49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
defmodule UcxRemote.User do
use UcxRemote.Web, :model
schema "users" do
has_many :user_roles, UcxRemote.UserRole, on_delete: :delete_all, on_replace: :delete
has_many :roles, through: [:user_roles, :role], on_delete: :delete_all, on_replace: :delete
field :first_name, :string
field :last_name, :string
field :email, :string
timestamps
end
def changeset(model, params \\ %{}) do
model
|> cast(params, [:first_name, :last_name, :email])
|> validate_required([:first_name, :last_name, :email])
|> cast_assoc(:user_roles, required: false)
|> unique_constraint(:email)
end
enddefmodule UcxRemote.UserRole do
use UcxRemote.Web, :model
schema "roles_users" do
belongs_to :role, UcxRemote.Role
belongs_to :user, UcxRemote.User
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:role_id, :user_id])
|> cast_assoc(:role, required: true)
end
enddefmodule UcxRemote.Role do
use UcxRemote.Web, :model
schema "roles" do
has_many :user_roles, UcxRemote.UserRole, on_delete: :delete_all
has_many :users, through: [:user_roles, :user], on_delete: :delete_all
field :name, :string
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name])
|> validate_required([:name])
end
end--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/361ed2ba-271b-4e40-97d2-c57592b0a8a9%40googlegroups.com.
BTW, the example in the book assumes that the new records are inserted.
%{"user" => %{"roles" => [%{"id" => 1}]}} I get "name can't be blank" and when I give a name, it inserts a Role.
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/1a35f115-3405-465d-842e-46a3b236e1a5%40googlegroups.com.
defmodule ContactDemo.User do
use ContactDemo.Web, :model
use Coherence.Schema
alias ContactDemo.AppConstants
schema "users" do
field :name, :string, null: false
field :username, :string, null: false
field :email, :string, null: false
field :active, :boolean, null: false, default: true
field :expire_on, Timex.Ecto.Date
#has_many :users_roles, ContactDemo.UserRole
#has_many :roles, through: [:users_roles, :role]
#
many_to_many :roles, ContactDemo.Role, join_through: ContactDemo.UserRole
coherence_schema
timestamps
end
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(name email username active expire_on) ++ coherence_fields)
|> cast_assoc(:roles)
|> validate_required([:name, :email, :username, :active]) # TODO: Add 'expire_on'
|> validate_format(:name, AppConstants.name_format)
|> validate_length(:name, min: 1, max: 255)
|> validate_format(:username, AppConstants.username_format)
|> validate_length(:username, min: 1, max: 255)
|> validate_length(:email, min: 1, max: 255)
|> unique_constraint(:username, name: :users_username_index)
|> validate_format(:email, AppConstants.email_format)
|> unique_constraint(:email, name: :users_email_index)
|> validate_coherence(params)
end
end
defmodule ContactDemo.Role do
use ContactDemo.Web, :model
alias ContactDemo.AppConstants
schema "roles" do
field :name, :string, null: false
many_to_many :users, ContactDemo.User, join_through: ContactDemo.UserRole
timestamps
end
@required_fields ~w(name)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ %{}) do
model
|> cast(params, @required_fields, @optional_fields)
|> validate_required(:name)
|> validate_format(:name, AppConstants.name_format)
|> validate_length(:name, min: 1, max: 255)
|> unique_constraint(:name, name: :roles_name_index)
end
endUser.changeset(%User{}, %{"roles" => [%{"id" => "2"}], "name" => "c", "password" => "password", "email" => "te...@test.com", "username" => "test"})
#Ecto.Changeset<action: nil,
changes: %{email: "te...@test.com", name: "c", password: "password",
roles: [#Ecto.Changeset<action: :insert, changes: %{},
errors: [name: {"can't be blank", []}], data: #ContactDemo.Role<>,
valid?: false>], username: "test"}, errors: [], data: #ContactDemo.User<>,
valid?: false>User.changeset(%User{}, %{"user_roles" => [%{"role_id" => "2"}], "name" => "c", "password" => "password", "email" => "te...@test.com", "username" => "test"})
#Ecto.Changeset<action: nil,
changes: %{email: "te...@test.com",
encrypted_password: "$2b$12$AH67FYsDHT2HG/mv1kwVc.giS7w.bNE3wKmCKpK5WX79GpXveuJ/W",
name: "c", password: "password", username: "test"}, errors: [],
data: #ContactDemo.User<>, valid?: true> model
|> cast(params, ~w(name email username active expire_on) ++ coherence_fields)
|> cast_assoc(:user_roles)User.changeset(%User{}, %{"user_roles" => [%{"role_id" => "2"}], "name" => "c", "password" => "password", "email" => "te...@test.com", "username" => "test"})
** (ArgumentError) cannot cast assoc `user_roles`, assoc `user_roles` not found. Make sure it is spelled correctly and properly pluralized (or singularized)
(ecto) lib/ecto/changeset.ex:606: Ecto.Changeset.relation!/4
(ecto) lib/ecto/changeset.ex:554: Ecto.Changeset.cast_relation/4
(contact_demo) web/models/user.ex:38: ContactDemo.User.changeset/2
User.changeset(%User{}, %{"roles" => [%{"id" => "2", "name" => "HI"}], "name" => "c", "password" => "password", "email" => "te...@test.com", "username" => "test"})
#Ecto.Changeset<action: nil,
changes: %{email: "te...@test.com",
encrypted_password: "$2b$12$YAWASYtXWiLVs7viaUAKVe4YYX9cxnC9PMGSKsgzSSNYflFW34Inm",
name: "c", password: "password",
roles: [#Ecto.Changeset<action: :insert, changes: %{name: "HI"}, errors: [],
data: #ContactDemo.Role<>, valid?: true>], username: "test"}, errors: [],
data: #ContactDemo.User<>, valid?: true>User.changeset(%User{roles: [role]}, %{"roles" => [%{"id" => "2", "name" => "HI"}], "name" => "c", "password" => "password", "email" => "te...@test.com", "username" => "test"})
#Ecto.Changeset<action: nil,
changes: %{email: "te...@test.com",
encrypted_password: "$2b$12$Ak3S6zvmXLpHlqRspro5tO1fbz6Y6ehnUnCN5TBegdSS5QrCGDPGO",
name: "c", password: "password",
roles: [#Ecto.Changeset<action: :update, changes: %{name: "HI"}, errors: [],
data: #ContactDemo.Role<>, valid?: true>], username: "test"}, errors: [],
data: #ContactDemo.User<>, valid?: true>User.changeset(%User{roles: [role]}, %{"roles" => [%{"id" => "2"}], "name" => "c", "password" => "password", "email" => "te...@test.com", "username" => "test"})
#Ecto.Changeset<action: nil,
changes: %{email: "te...@test.com",
encrypted_password: "$2b$12$Z32J7S5Uk47uXAf0PahA7e8n0CTjRD2tXWNL0ob4Qiz8h.iNrjN12",
name: "c", password: "password", username: "test"}, errors: [],
data: #ContactDemo.User<>, valid?: true>User.changeset(%User{}, %{"roles" => [%{"id" => "2"}], "name" => "c", "password" => "password", "email" => "te...@test.com", "username" => "test"})
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/179a5f94-47ba-46b8-8873-f4427fea6bd8%40googlegroups.com.
|> cast_assoc(:user_roles, required: false, allowed_join: [1, 2, 5], allow_creation: true)
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/3e533e08-52ae-4cf2-b3af-1593a2b5ab27%40googlegroups.com.
Yes, I have also found it odd that the effect of each schema type is the same in some ways but different in others. It seems weird to me that in one scenario the reasoning for not allowing cast_asoc to work is that it is a security issue, but in another scenario the security issue seems to be ignored. I would personally prefer a consistent api, that worked with the cast_assoc.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-ecto" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-ecto/s4heBrGN9J0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4%2BzRm4RJ9cSych8FD2QRBpE%2BuiwPwA6HSOkAM4wMPoPLA%40mail.gmail.com.
To unsubscribe from this group and all its topics, send an email to elixir-ecto+unsubscribe@googlegroups.com.