defmodule Demo.Post do
# use Demo.Web, :model
use Ecto.Schema
schema "posts" do
field :title, :string
field :lock_version, :integer, default: 1
timestamps
end
@required_fields ~w(title lock_version)
@optional_fields ~w()
def changeset(model, params \\ :empty) do
model
|> Ecto.Changeset.cast(params, @required_fields, @optional_fields)
|> Ecto.Changeset.optimistic_lock(:lock_version)
end
end
def update(conn, %{"id" => id, "post" => post_params}) do
post = Repo.get!(Post, id)
changeset = Post.changeset(post, post_params)
IO.inspect changeset
case Repo.update(changeset) do
{:ok, post} ->
conn
|> put_flash(:info, "Post updated successfully.")
|> redirect(to: post_path(conn, :show, post))
{:error, changeset} ->
render(conn, "edit.html", post: post, changeset: changeset)
end
end
%Ecto.Changeset{action: nil, changes: %{lock_version: 4, title: "post a modified again"}, constraints: [], errors: [], filters: %{lock_version: 3}, model: %Demo.Post{__meta__: #Ecto.Schema.Metadata<:loaded>, id: 5, inserted_at: #Ecto.DateTime<2016-04-10T12:21:14Z>, lock_version: 3, title: "post a modified", updated_at: #Ecto.DateTime<2016-04-10T12:22:10Z>}, optional: [], opts: [], params: %{"lock_version" => "2", "title" => "post a modified again"}, prepare: [], repo: nil, required: [:title, :lock_version], types: %{id: :id, inserted_at: Ecto.DateTime, lock_version: :integer, title: :string, updated_at: Ecto.DateTime}, valid?: true, validations: []}
--
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/8bb09faf-1c3c-40cf-8a2c-a335c35ee687%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
field :lock_version, :integer, default: 1
def create(conn, %{"post" => post_params}) do
changeset = Post.changeset(%Post{}, post_params)
IO.inspect changeset
case Repo.insert(changeset) do
{:ok, _post} ->
conn
|> put_flash(:info, "Post created successfully.")
|> redirect(to: post_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
[info] POST /posts[debug] Processing by Demo.PostController.create/2 Parameters: %{"_csrf_token" => "bg8ddS8eHFpUHWMcJm96AzkpXBUUNgAA7Nd9CL7b9JTnG+/QJN/rfw==", "_utf8" => "✓", "post" => %{"title" => "post x"}} Pipelines: [:browser]%Ecto.Changeset{action: nil, changes: %{lock_version: 2, title: "post x"}, constraints: [], errors: [], filters: %{lock_version: 1}, model: %Demo.Post{__meta__: #Ecto.Schema.Metadata<:built>, id: nil, inserted_at: nil, lock_version: 1, title: nil, updated_at: nil}, optional: [:lock_version], opts: [], params: %{"title" => "post x"}, prepare: [], repo: nil, required: [:title],
types: %{id: :id, inserted_at: Ecto.DateTime, lock_version: :integer, title: :string, updated_at: Ecto.DateTime}, valid?: true, validations: []}
[debug] INSERT INTO "posts" ("inserted_at", "updated_at", "lock_version", "title") VALUES ($1, $2, $3, $4) RETURNING "id" [{{2016, 4, 10}, {14, 3, 28, 0}}, {{2016, 4, 10}, {14, 3, 28, 0}}, 2, "post x"] OK query=0.9ms
field :lock_version, :integer, default: 0
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/51386149-1ba3-4722-b035-df742e9066d6%40googlegroups.com.