~iany/ Menu
  • Series
  • Tags
  1. Home
  2. Tags
  3. Active Record

Active Record

A collection of 2 articles

ActiveRecord includes and preload

ActiveRecord has two query methods to eager load associations, includes and preload. Although the documentation of preload says Allows preloading of args, in the same way that includes does. Indeed the two methods have some differences. In simple words, prefer includes to eager load associations. Use preload only when you want to customize select columns, or you meet error “Can not eagerly load the polymorphic association”.

Updated Nov 19, 2025  •  2 min read

ActiveRecord uniq, count and distinct

ActiveRecord has two methods to remove duplicates. Method uniq and option distinct: true in method count. I thought uniq.count and count(distinct: true) were identical. Indeed, uniq.count still counts duplicates, and count(distinct: true) must be used here. In simple words, use uniq to get unique result set, use count(distinct: true) to count unique result.

Updated Nov 19, 2025  •  1 min read

© 2026  •  ~iany/  •  CC-BY-SA 4.0