# Config.after\_initialize and development mode

**URL:** <https://www.ruby-forum.com/t/config-after-initialize-and-development-mode/146741>\
**Category:** Rails\
**Created:** [August 24, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/config-after-initialize-and-development-mode/146741 "2008-08-24T23:06:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mveerman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@mveerman](https://www.ruby-forum.com/u/mveerman)\
**Post date:** [August 24, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/config-after-initialize-and-development-mode/146741/1 "2008-08-24T23:06:12Z")

</div>

Hi,

I’m using activemerchant and setting up a class variable using  
config.after\_initialize. It works great for the first request, but  
then the variable is nil.

config.after\_initialize do  
ActiveMerchant::Billing::Base.mode = :test  
OrderTransaction.gateway =  
ActiveMerchant::Billing::PaypalGateway.new(…)  
end

I’m assuming this is is due to the model reloading after each request  
in development mode, right? When that happens OrderTransaction.gateway  
is nilified.

Is there a way to prevent a model from being reloaded in development  
mode, and is this the best way to handle the problem.

Thanks!  
Mark

---

<div class="post-metadata">

**Author:** ![mveerman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@mveerman](https://www.ruby-forum.com/u/mveerman)\
**Post date:** [August 24, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/config-after-initialize-and-development-mode/146741/2 "2008-08-24T23:10:53Z")

</div>

On 24 Aug 2008, at 21:46, mveerman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ActiveMerchant::Billing::PaypalGateway.new(…)  
> end
> 
> I’m assuming this is is due to the model reloading after each request  
> in development mode, right? When that happens OrderTransaction.gateway  
> is nilified.

Sounds plausible

> Is there a way to prevent a model from being reloaded in development  
> mode, and is this the best way to handle the problem.

Add the model file to Dependencies.load\_once\_paths (may cause problems  
if that’s a model with associations to other models etc…) or use  
config.to\_prepare (which executes before each request us dispatched)

Fred

---

<div class="post-metadata">

**Author:** ![mveerman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@mveerman](https://www.ruby-forum.com/u/mveerman)\
**Post date:** [August 25, 2008, 1:37am UTC](https://www.ruby-forum.com/t/config-after-initialize-and-development-mode/146741/3 "2008-08-25T01:37:41Z")

</div>

That worked perfectly!

I decided to use config.to\_prepare in environments/development.rb  
like:  
config.to\_prepare do  
ActiveMerchant::Billing::Base.mode = :test  
OrderTransaction.gateway =  
ActiveMerchant::Billing::PaypalGateway.new(…)  
end

and left config.after\_initialize in environments/production.rb  
I haven’t tested the production environment yet, but I’m assuming I  
won’t run into the same problem.

Thanks for the help,

Mark

On Aug 24, 5:09 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
