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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
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
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
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...
3271 126479 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement