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!

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).

9 Likes