Forum: Ruby on Rails Overloading #initialize in a model

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Nick (Guest)
on 2008-11-01 18:13
(Received via mailing list)
I'm using WhinyProtectedAttributes:

Say I have this model:

class Person < ActiveRecord::Base
  attribute_accessible :name

If I then create a Person with an invalid attribute, an exception is
raised. For example: :name => 'Bob', :asdf => 'asdf'

What I'd like to do is, in the Person model, modify the hash of
attributes before they're set. This is what I came up with:

class Person < ActiveRecord::Base
  attribute_accessible :name

  def initialize(attribs = {})
    attribs_that_exist = AccessibleModelAttributes.extract self.class,
    super attribs_that_exist

Am I on the right track? Cheers,
This topic is locked and can not be replied to.