# Five Active Record Features You Should Be Using

DevFeed: [Five Active Record Features You Should Be Using](<https://devfeed.tech/articles/five-active-record-features-you-should-be-using-21050.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/11/15/five-active-record-features-you-should-be-using/>)

Published: 2015-11-15T12:00:00Z

Content type: article

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Databases](<https://devfeed.tech/topics/databases.md>), [Persistence](<https://devfeed.tech/topics/persistence.md>), [Query (disambiguation)](<https://devfeed.tech/topics/query.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [database](<https://devfeed.tech/tags/database.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>)

## AI overview

This article explains how to use Active Record more effectively in Ruby on Rails applications. The supplied text discusses nested queries that let the database handle memory allocation and optimization, and introduces DRY scopes for reusable query logic.

## Source excerpt

Active Record is responsible for communicating with the persistence layer by default in Ruby on Rails applications. Using Active Record effectively and efficiently can greatly improve an application's code. In Ruby on Rails 4.0, some material changes have been made to Active Record. Understanding these changes, and how they are best utilized is important for any Rails developer. To help explain these concepts, we can assume a Ruby on Rails application "booksandreviews.com" exists with three models: class Book < ActiveRecord::Base belongs_to :author has_many :reviews end class Author < ActiveRecord::Base has_many :books end class Review < ActiveRecord::Base belongs_to :book end The smart people over at "booksandreviews.com" need to know what the state of their data to build metrics and make sales. Metrics need data and Active Record will be used to fetch that data. 1. Nested Queries When doing database queries, the fewer the better. Since Active Record is responsible of crafting these queries, it is important to make sure it has all the help it needs. For simple queries this is rarely an issue, but more complex requirements can lead to sub-optimal results. One day, Tim from sales comes rampaging through the office convinced that there must be a bug in the system. A recent sale for "booksandreviews.com" did not go well and he wants answers. Tim wants an analysis run. He wants all reviews published today, which are for books published in 2015. Without too much thought, this approach seems reasonable: book_ids = Book.where(publish_year: '2015').map(&:id) # => SELECT "books".* FROM "books" WHERE (publish_year = '2015') reviews = Review.where(publish_date: '2015-11-15', book_ids: book_ids).to_a # => SELECT "reviews".* FROM "reviews" WHERE "reviews"."publish_date" = '2015-11-15' AND "reviews"."book_ids" IN (1, 2, 3) This will load the desired books, extract their id's, and pass that result to the Review query. Not only does this generate two queries, it also wastes memory