dogweather

dogweather

Could someone speak to ActiveRecord vs. Ecto queries, especially relative date comparisons?

I’m deciding whether to write a little reporting library in Rails or Elixir+Ecto. Here’s an ad-hoc query in the Rails console:

SearchLog
  .where("vip_status LIKE 'vip: yes%'")
  .where('created_at > ?', 3.days.ago)
  .pluck(:vip_status)

So, two questions: :slight_smile:

  1. How would I write the LIKE and 3.days.ago with Ecto?
  2. I realize that Elixir+Ecto will be more verbose. And also probably, have a greater mental overhead due to knowing in which modules various functions are found. From your experience, is the trade-off worth it? Are there benefits of the repo pattern over active record? How does the Elixir console compare to Pry for writing ad hoc queries?

Personally, I get high productivity from the Rails console. The fluent OO API really helps.

Thanks!

Most Liked

sodapopcan

sodapopcan

Ecto has relative date functions (including ago) and a like function. With your example it’s actually not really more verbose than AR:

from s in SearchLog,
  where: like(s.vip_status, "vip: yes%"),
  where: s.inserted_at > ago(3, "day"),
  select: %{vip_status: s.vip_status}

(I wrote that on the fly so sorry if there is an error)

I obviously don’t know anything about the lib you want to write, but in my experience having done lots of reporting with Rails, you end up writing a bunch of raw SQL, so Ecto would likely be a better overall experience (I have yet to do any reporting with Ecto).

The console is a bit more of a pain out of the box, but that can be remedied with a good .iex.exs. Alias all the stuff you use regularly and require Ecto.Query (or even import it if you’re feeling saucy).

10
Post #2

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
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
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement