# Overloading #initialize in a model

**URL:** <https://discuss.rubyonrails.org/t/overloading-initialize-in-a-model/35954>\
**Category:** rubyonrails-talk\
**Created:** [November 1, 2008, 4:13pm UTC](https://discuss.rubyonrails.org/t/overloading-initialize-in-a-model/35954 "2008-11-01T16:13:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick5](https://avatars.discourse-cdn.com/v4/letter/n/bcef8e/32.png) [@Nick5](https://discuss.rubyonrails.org/u/Nick5)\
**Post date:** [November 1, 2008, 4:13pm UTC](https://discuss.rubyonrails.org/t/overloading-initialize-in-a-model/35954/1 "2008-11-01T16:13:13Z")

</div>

I'm using WhinyProtectedAttributes: [http://henrik.nyh.se/2007/10/whiny-protected-attributes](http://henrik.nyh.se/2007/10/whiny-protected-attributes)

Say I have this model:

class Person \< ActiveRecord::Base &nbsp;&nbsp;attribute\_accessible :name end

If I then create a Person with an invalid attribute, an exception is raised. For example: &nbsp;&nbsp;Person.new :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 &nbsp;&nbsp;attribute\_accessible :name

&nbsp;&nbsp;def initialize(attribs = {}) &nbsp;&nbsp;&nbsp;&nbsp;attribs\_that\_exist = AccessibleModelAttributes.extract self.class, attribs &nbsp;&nbsp;&nbsp;&nbsp;super attribs\_that\_exist &nbsp;&nbsp;end end

Am I on the right track? Cheers, Nick
