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 https://hexdocs.pm/iex/IEx.html#module-the-iex-exs-file

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

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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement