Allow ActiveRecord::Core#slice to take array of attrs (like ActiveRecord::QueryMethods#select)

20 views
Skip to first unread message

Cohen Carlisle

unread,
Oct 19, 2016, 8:26:17 PM10/19/16
to Ruby on Rails: Core
Imagine you have some code as follows:

class User < ApplicationRecord
  PUBLIC_FIELDS
= %i(foo bar baz).freeze

  scope
:public_fooed, -> { select(PUBLIC_FIELDS).where(foo: true) }

 
def publicize
   slice
(*PUBLIC_FIELDS)
 
end
end

It slightly annoys me that to filter attributes at the model class level (while doing a `::select`), you don't need to splat `PUBLIC_FIELDS`, but you do when doing it at the model object level (with `#slice`).

We could safely flatten the arguments to `#slice` to remove the need to splat since ActiveRecord::Core#slice will never have attributes (or methods) that are arrays. This would bring it in line with `::select`

Thoughts?
Reply all
Reply to author
Forward
0 new messages