Skip to main content
  1. Posts/

ActiveRecord Except

·375 words·2 mins·
Application Development
Table of Contents

August 19 was Whyday, and to commemorate it, I decided to write a gem called activerecord-except.

What is it?
#

activerecord-except is a Ruby gem that extends the functionality of ActiveRecord, allowing you to select all the fields from a table, except the ones that you specify. For example, if you have a table users.

development_db=# \d users
      Table "public.users"
            Column             | 
-------------------------------+-
 id                            | 
 username                      | 
 password                      | 
 email                         | 
 first_name                    | 
 last_name                     | 
 phone                         | 
 ...
 created_at                    | 
 updated_at                    | 

And you want to get all the fields except for the password, you’d have to pass each of them in your select clause like so

User.all.select(:id,
                :username,
                :email,
                :first_name,
                :last_name,
                :phone,
                ...
                :created_at,
                :updated_at)

Instead, using activerecord-except, can simplify your statement by saying only the field you don’t want, in this case, the password one

User.all.except(:password)

How does it work?
#

Under the hood, the except clause makes use of the traditional select clause. So our previous example will produce the following query

SELECT "users"."id",
       "users"."username",
       "users"."first_name",
       "users"."last_name",
       "users"."phone",
       "users"."created_at",
       ...
       "users"."updated_at"
  FROM "users"

This is because the SQL language doesn’t provide such functionality out of the box.

I don’t know what is the reason for this. I can only speculate that it’s to be more explicit and not be caught by surprise if a field in a table gets added/deleted/changed. However, * is also wildly used. In Rails for example, it is what you get, when you don’t specify a select clause in your query.

The way I managed to make it work is by adding a method to ActiveRecord::Relation which asks the model for all its attributes and rejecting those that match with the ones passed as arguments.

klass._default_attributes
     .keys.map(&:to_sym)
     .reject { |attr| fields.include?(attr) }
As you can see, I’m using _default_attributes which starts with an underscore. This can mean that the method is not intended to be relied upon.

Whether or not you might want to use in production, I leave up to you, where I really see the benefit of activerecord-except is for writing one-off scripts to extract data, because it makes them much easier to read.

Want to give it a try?
#

You can install it from rubygems or you can check the source code on GitHub.

Reply by Email

Related

Vortex Core Mechanical Keyboard Review
·770 words·4 mins
Hardware

I got myself a new keyboard for my birthday, the Vortex Core. I wanted a mechanical keyboard that I could take everywhere with me. Being a 40% keyboard, I expected it to over deliver on the portable side, what I didn’t expect, is that I’d enjoy using this tiny keyboard so much, even for extended periods of time.

Running a Patched Ruby on Heroku
·660 words·4 mins
Plataforms

You use a PaaS because you want all the underlying infrastructure and configuration of your application to be hidden from you. However, there are times when you are forced to look deeper into the stack. In this article I want to share how simple it is to run a patched version of Ruby on Heroku.

Ruby's DATA Stream
·285 words·2 mins
Programming Languages

The STDIN and ARGF streams are commonly used in Ruby, however there’s also the less popular DATA one. This is how it works and some examples in the wild.