Skip to content

Instantly share code, notes, and snippets.

@zdennis
Created November 28, 2018 18:35
Show Gist options
  • Save zdennis/f832656eb596c8c7f379c7598a6bc45b to your computer and use it in GitHub Desktop.
Save zdennis/f832656eb596c8c7f379c7598a6bc45b to your computer and use it in GitHub Desktop.
ActiveRecord: Limiting columns selected by default
class SomeModel < ::ActiveRecord::Base
default_scope -> { select('id') } }
end
# only select id
SomeModel.where(name: 'Wibble').all
# override and select all columns
SomeModel.unscoped.where(name: 'Wibble').all
# override select for specific columns
SomeModel.select('id, column1, column2, name').where(name: 'Wibble').all
# One thing not represented is adding to the default selected columns.
# Right now, when overriding select you have to supply all of the columns you want.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment