Eiji

Eiji

[SOLVED] When Ecto.Changeset action is set?

When I’m creating Post insert changeset from iex:

new_post = %MyProject.Post{}
changeset = Ecto.Changeset.cast(new_post, %{body: "Body", title: "Title"}, [:title, :body])
#Ecto.Changeset<action: nil, changes: %{body: "Body", title: "Title"},
 errors: [], data: #First.Post<>, valid?: true>

When I’m creating Post update changeset from iex:

post = MyProject.Repo.get(MyProject.Post, 5)
changeset = Ecto.Changeset.cast(post, %{body: "Body", title: "Title"}, [:title, :body])    
#Ecto.Changeset<action: nil, changes: %{body: "Body", title: "Title"},
errors: [], data: #First.Post<>, valid?: true>

It always returns action: nil. When Ecto.Changeset action is set?

Marked As Solved

michalmuskala

michalmuskala

When you pass the changeset to one of the Repo functions.

Also Liked

michalmuskala

michalmuskala

I’m not sure that’s the right approach - why not create multiple changeset functions?

def create_changeset(schema, params) do
  schema
  |> common_changeset(params)
  |> create_specific_operations
end

def update_changeset(schema, params) do
  schema
  |> common_changeset(params)
  |> update_specific_operations
end

def common_changeset(schema, params) do
  # ...
end
Eiji

Eiji

You mean: delete(!), insert(!) and update(!), right?
I want to have it set in changeset model function (before affect database) and perform some actions (in cond do statement).
Is it possible? Or I need set it manually? Or maybe one more parameter in changeset function for cond do statement is better solution?

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement