Created
November 28, 2018 18:35
-
-
Save zdennis/f832656eb596c8c7f379c7598a6bc45b to your computer and use it in GitHub Desktop.
ActiveRecord: Limiting columns selected by default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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