# Overloading #initialize in a model

**URL:** <https://www.ruby-forum.com/t/overloading-initialize-in-a-model/152300>\
**Category:** Rails\
**Created:** [November 1, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/overloading-initialize-in-a-model/152300 "2008-11-01T17:13:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [November 1, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/overloading-initialize-in-a-model/152300/1 "2008-11-01T17:13:59Z")

</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  
attribute\_accessible :name  
end

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

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

Am I on the right track? Cheers,  
Nick
