Passive Columns
A gem that extends Active Record
to retrieve columns from DB on demand.
Works with Rails
>= 6.1 and Ruby
>= 2.7
Usage
class Page < ApplicationRecord
include PassiveColumns
passive_columns :huge_article
end
ActiveRecord::Relation
now retrieves all the columns except the passive ones by default.
article = Page.where(status: :active).to_a
If you specify the columns via select it retrieves only the specified columns and nothing more.
page = Page.select(:id, :title).take
page.to_json
But you still has an ability to retrieve the passive column on demand
page.huge_article
'Some huge article...'
page.to_json
page.huge_article
Another way to get columns on demand is to use the load_column
method.
This method loads a column value, if not already loaded, from the database
regardless of whether the column is added to passive_columns
or not.
class User < ActiveRecord::Base
include PassiveColumns
end
user = User.select('id').take!
user.name
user.load_column(:name)
'John'
user.load_column(:name)
'John'
user.name
'John'
By the way, it uses the Rails' .pick
method to get the value of the column under the hood
Important
If you want passive_columns
to skip validation rules specific to the columns you exclude.
(in case they were not retrieved / modified)
validates :huge_article, presence: true
You must declare validation rules for passive_columns
separately
class Page < ActiveRecord::Base
include PassiveColumns
passive_columns :huge_article
validates :name, presence: true
validates :huge_article, presence: true
end
class Page < ActiveRecord::Base
include PassiveColumns
passive_columns :huge_article
validates :name, :huge_article, presence: true
end
Installation
Add this line to your Gemfile:
gem "passive_columns"
And then execute:
$ bundle install
Or install it yourself as:
$ gem install passive_columns
Motivation
There are situations when you have an Active Record
model with columns
that you don't want to fetch from a DB every time you manipulate the model.
What options do you have?
scope :skip_retrieving, ->(*v) { select(column_names.map(&:to_sym) - Array.wrap(v)) }
scope :only_main_columns, -> { select(%w[id name description uuid]) }
default_scope { :only_main_columns }
At first glance, it seems like a good solution.
Until you realize that you cannot manipulate the model without the columns you skipped, as there are validation rules related to them.
class Project < ActiveRecord::Base
scope :only_main_columns, -> { select(%w[id name description uuid]) }
validates :id, :name, presence: true
validates :settings, presence: true
end
p = Project.only_required_columns.take
p.update!(name: 'New name')
One way to avoid this is to check for the presence of the attribute before validating it.
validates :huge_article, presence: true, if: -> { attributes.key?('huge_article') }
Unfortunately, boilerplate code is needed for such a simple task.
But the only thing you wanted was to exclude some columns and be able to manipulate a model without extra steps.
By the way, after doing those steps, you still cannot retrieve the column when you need it after loading the scoped model...
So, passive_columns
tries to solve this problem by allowing you to exclude columns from the selection and also allowing you to retrieve them on demand when needed.
Inspiration
There are similar gems that were relatively popular but are no longer supported. Let's give them the honor they deserve:
License
The gem is available as open source under the terms of the MIT License.