Sunfox


Journal

Archives du 9 mars 2021

Arel through examples

9 mars 2021 , , , ,

When a database query cannot be described using Ruby on Rails’ short ActiveRecord syntax, consider building your queries using Arel instead of going for raw SQL strings.

This has the following advantages:

  • Escaping by default: this helps not end up with SQL injections. We don’t like those.
  • Full names: using "users"."email" instead of email in your queries helps you be future-proof by making queries that won’t break when you merge queries together or add columns with conflicting names.
  • Composable: Arel makes allows you to merge advanced conditions by manipulating Ruby objects.
  • Uniform queries: it ensures that the generated SQL always looks the same. For example "users"."email" ASC instead of email asc. This makes it easier to search through logs and can help some caching mechanisms.
Suite de l’article…

👨🏻‍🦰 Sunny Ripert

est un développeur web vivant à ParisContactArchives

Textes et contenus sous licence Creative Commons.