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

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
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
_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
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement