Rails 5 adds ignored_columns for Active Record

Hitesh Rawal

By Hitesh Rawal

on May 24, 2016

This blog is part of our  Rails 5 series.

Sometimes we need to ignore a database column. However Rails 4.x doesn't have any officially defined method which ignores a database column from Active Record. We can apply our patch on model to ignore certain columns.

1class User < ActiveRecord::Base
2
3  # Ignoring employee_email column
4  def self.columns
5    super.reject {|column| column.name == 'employee_email'}
6  end
7
8end

Rails 5 added ignored_columns

Rails 5 has added ignored_columns to ActiveRecord::Base class.

Here is revised code after using ignored_columns method.

1class User < ApplicationRecord
2  self.ignored_columns = %w(employee_email)
3end

If you liked this blog, you might also like the other blogs we have written. Check out the full archive.

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, React.js, React Native, remote work,open source, engineering & design.