yeroc

yeroc

Ecto Changeset Updates

Hello!

Trying to fully wrap my head around Ecto when updating a struct value. I know this is super basic and simple; forgive my naivete :sweat_smile:

Generated a basic Phoenix / Elixir CRUD app to play with and am testing out the generated Phoenix functions in IEx. The one that I can’t seem to wrap my head around is update. Here is the function-

def update_post(%Post{} = post, attrs) do
	post
	|> Post.changeset(attrs)
	|> Repo.update()
end

Here is the schema changeset

  def changeset(post, attrs) do
    post
    |> cast(attrs, [:title, :body])
    |> validate_required([:title, :body])
  end

Let’s say I have this existing post-

 %BlogApp.Posts.Post{
    __meta__: #Ecto.Schema.Metadata<:loaded, "posts">,
    body: "My very first Post!",
    id: 1,
    inserted_at: ~N[2022-09-21 03:21:59],
    title: "A great post",
    updated_at: ~N[2022-09-21 03:21:59]
  }

How can this post be updated in IEx using the existing Phoenix generated function above? I don’t quite understand how to select and match on the struct entry for the first value to feed to update_post/2

For example, I’m trying-

iex(9)> post = %{body: "My very first Post!", title: "A great post"}
iex(10)> Posts.update_post(post, %{body: "updated", title: "updated"})
...(11)> 
** (FunctionClauseError) no function clause matching in BlogApp.Posts.update_post/2

    The following arguments were given to BlogApp.Posts.update_post/2:

        # 1
        %{body: "My very first Post!", title: "A great post"}

        # 2
        %{body: "updated", title: "updated"}

    Attempted function clauses (showing 1 out of 1):

        def update_post(%BlogApp.Posts.Post{} = post, attrs)

    (blog_app 0.1.0) lib/blog_app/posts.ex:70: BlogApp.Posts.update_post/2

I’m just not grasping how to match the function clause!

Thanks for any help! :slight_smile:

Marked As Solved

yeroc

yeroc

Great catch! :person_facepalming: Also realized after the fact I wasn’t supplying the primary key (id) for the existing post.

Also Liked

experimentix

experimentix

I’m still learning elixir and phoenix but it seems you are passing a map instead of a struct Post to update post

changing

post = %{body: "My very first Post!", title: "A great post"}

to

post = %Post{body: "My very first Post!", title: "A great post"}

will match in

def update_post(%Post{} = post, attrs) do

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
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
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