I hava users table and it has a field password_digest and I am working with an API and I want to appply this:
Users.all.select(*User.attribute_names.reject { |attr| attr == 'password_digest' })
but I have to write this in every query I do, how can I set this as a default behavior.
You can use default scope:
class User < ActiveRecord::Base
default_scope select(*User.attribute_names.reject { |attr| attr == 'password_digest' })
end
Another way :
class User < ActiveRecord::Base
default_scope select(User.column_names - ["password_digest"])
end
Reference:
https://apidock.com/rails/ActiveRecord/Base/default_scope/class