[Rails] Re: Hiding Model Attribute

Drew Raines
Wed, 02 Aug 2006 23:24:11 -0000

Doug Selph wrote:

> I have a model object for which I would like to effectively
> hide one attribute (a password hash) so that it is never
> returned by a find_* call. I have tried several unsuccessful
> means. Is there a simple way to do this that I am
> overlooking?

Since the attribute method doesn''t actually exist, you can only
preempt calls to #method_missing by creating a real method,
like another person mentioned.  However, I would create a
crutch for the rest of the class so you don''t have to muck with
@attributes just to get to the password:

  def method_missing(method_id, *args, &block)
    method_id == :password ? "No soup for you" : super
  end

  def password
    read_attribute(:password)
  end

  def password=(pass)
    write_attribute(:password, pass)
  end

  private :password, :password
-Drew