sergio

sergio

Updating a field using Ecto one-liner?

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?

Ecto.Changeset.change(MyApp.User |> where(email: "foobar"), email: "barbaz") |> MyApp.Repo.update

Marked As Solved

outlog

outlog

you can do:

Ecto.Changeset.change( MyApp.Repo.get_by(MyApp.User, email: "foobar@email.com"), %{email: "hello@email.com"}) |> MyApp.Repo.update()

but piping is much nicer:

MyApp.Repo.get_by(MyApp.User, email: "foobar@email.com")
|> Ecto.Changeset.change(%{email: "hello@email.com"})
|> MyApp.Repo.update()

I would not do this for production code though - I would have error handling, and explicit changeset with the needed validations etc etc.

Also Liked

outlog

outlog

I would imagine this is console/iex work - that can seem a bit tedious (at first) in iex compared to rails (imho)

one trick is to have an .iex.exs file in your project root IEx — IEx v1.20.2

say for this example you had the following in it:

import Ecto.Changeset
alias MyApp.Repo
alias MyApp.User

then you can do:

Repo.get_by(User, email: "foobar@email.com")
|> change(%{email: "hello@email.com"})
|> Repo.update()

and you are not that many characters away from the OO rails way.. but again not for production code..

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Ecto has made the decision to clearly separate data from action, whereas in an OO language like Ruby they’re tied together.

Consequently, you may need an extra line or two to do certain things via Ecto that you’d do from ActiveRecord. I don’t really see this as much of a problem, and it helps make it wildly clearer when and how database access actually happens.

Normal aliasing practices will also help a bit alias MyApp.Repo.

Ultimately though I’m not really sure what your question is. You ask “how” but then you provide an example that does exactly what you want. Is your question more “what’s the most succinct way I can do this?” ?

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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 Postgres...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement