# Five More Active Record Features You Should Be Using

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

Original publisher: [Read original article](<https://jakeyesbeck.com/2016/03/27/five-more-active-record-features-you-should-be-using/>)

Published: 2016-03-27T12:00:00Z

Content type: tutorial

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>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [code](<https://devfeed.tech/tags/code.md>), [databases](<https://devfeed.tech/tags/databases.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 tutorial presents additional ActiveRecord features for Ruby on Rails applications. It explains how pluck can reduce memory allocation when retrieving selected columns, how transactions can preserve atomic behavior when updates fail, and begins discussing model callbacks. The supplied article is truncated during the callbacks section.

## Source excerpt

In a previous post, I illustrated a few helpful ActiveRecord features. The entire API of ActiveRecord cannot possibly be contained within a single or even a handful of digestible posts; but, here are at least five more pieces of that massive API that some might find useful. Just like before, the example application used to demonstrate these features will be an imaginary Ruby on Rails application: "booksandreviews.com": 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 1. pluck Introduced in Ruby on Rails 4.0, the pluck method helps keep memory allocation to a minimum when returning results from ActiveRecord queries. A great use case for the pluck method is when a database table backing an ActiveRecord object has a very large number of columns. In this situation, returning an object's full dataset can be cause unnecessary memory allocation and potentially expensive deserialization (in the case of custom or JSON serialized columns). To get a list of book_ids from the 'fantasy' genre, the pluck method is a simple to use: Book.where(genre: 'fantasy').pluck(:id) # SELECT "books"."id" FROM "books" WHERE "books"."genre" = 'fantasy' => [1, 3, 45, ...] An important thing to recognize about the pluck method is its return value. Unlike its cousin, select, the pluck method does not return an ActiveRecord instance. Multiple column names may be passed to pluck and the values of these columns will be returned in a nested Array. The returned Array will maintain the order of columns to how they were requested: Book.where(genre: 'fantasy').pluck(:id, :title) # SELECT "books"."id", "books"."title" FROM "books" WHERE "books"."genre" = 'fantasy' => [[1, 'A Title'], [3, 'Another One']] While possible, it might not always be the best idea to request multiple columns in a single pluck call. Too many columns can produce an unruly result and nullify the performa