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

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
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
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
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
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement