# AR after\_initialize quandry

**URL:** <https://www.ruby-forum.com/t/ar-after-initialize-quandry/190047>\
**Category:** Rails\
**Created:** [June 12, 2010, 12:14am UTC](https://www.ruby-forum.com/t/ar-after-initialize-quandry/190047 "2010-06-12T00:14:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 12, 2010, 12:14am UTC](https://www.ruby-forum.com/t/ar-after-initialize-quandry/190047/1 "2010-06-12T00:14:00Z")

</div>

I have a case where I am abstracting the creation of models. After a  
fair bit of indirection, I eventually get around to making a .new call.

I need to be able to pass params to that .new call.

I know I can’t use def initialize() in an AR model (rather frustrating).

So, I have found some references to after\_initialize, but that doesn’t  
work either.

I see several threads on people trying to use after\_initialize, but no  
conclusive answers to the same errors I am seeing – undefined method  
`stringify\_keys!

Example refc: [Purpose of after\_initialize in ActiveRecord? - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/86173)

My after\_initialize is just this simple code:

def after\_initialize(asset\_path)  
@asset\_path = asset\_path  
end

So either a) what is the proper technique for using after\_initialize, or  
b) is there another way I can pass params to an AR model in a .new call  
?

Thx.

– greg willits

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 12, 2010, 12:57am UTC](https://www.ruby-forum.com/t/ar-after-initialize-quandry/190047/2 "2010-06-12T00:57:04Z")

</div>

> […]
> 
> > So either a) what is the proper technique for using after\_initialize, or  
> > b) is there another way I can pass params to an AR model in a .new call  
> > ?
> 
> Can’t you just call Model.new(:the =\> ‘params’, :go =\> ‘here’) ?

I’ve never used that form, but I should have remembered it.

For my immediate need that won’t work, but that’s a function of the  
domain problem in this instance (turns out what I wanted can’t be solved  
by passing params into new anyway). For general purpose needs, that  
would probably work, so I’ll implement it in the API for this abstrated  
system to at least support passsing attributes upon a new().

Thx.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 12, 2010, 12:20am UTC](https://www.ruby-forum.com/t/ar-after-initialize-quandry/190047/3 "2010-06-12T00:20:54Z")

</div>

Greg W. wrote:  
[…]

> So either a) what is the proper technique for using after\_initialize, or  
> b) is there another way I can pass params to an AR model in a .new call  
> ?

Can’t you just call Model.new(:the =\> ‘params’, :go =\> ‘here’) ?

> Thx.
> 
> – greg willits

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
