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

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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement