ActiveRecord : Hide column while returning object

activerecord, rails-activerecord, ruby-on-rails, ruby-on-rails-3

Solution

Using the built-in serialization, you can override the `as_json` method on your model to pass in additional default options:

class User < ActiveRecord::Base
  # ...
  def as_json(options = {})
    super(options.merge({ except: [:password, :oauth_token] }))
  end
end

There are probably better serialization tools out there - if you are looking for more fine-grained control I would recommend checking out `active_model_serializers` or `rabl`.

Problem

Is there an out-of-the-box way to always hide/remove a column (say, User.password) while returning an ActiveRecord object?

Original source