# How to add a initialize method when rails app starts?

**URL:** <https://www.ruby-forum.com/t/how-to-add-a-initialize-method-when-rails-app-starts/113953>\
**Category:** Rails\
**Created:** [September 6, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/how-to-add-a-initialize-method-when-rails-app-starts/113953 "2007-09-06T16:57:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ye\_Dingding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/49beb7/32.png) [@Ye\_Dingding](https://www.ruby-forum.com/u/Ye_Dingding)\
**Post date:** [September 6, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/how-to-add-a-initialize-method-when-rails-app-starts/113953/1 "2007-09-06T16:57:17Z")

</div>

I know the Rails::Initializer class process the initialization for apps.  
But  
i found the method is under the protection. That is,

Rails::Initializer.run do |config|  
end

when i invoke the run method, it then do process method which is the  
actually initializing method.

I want to know how can i extend the method without changing  
railties/lib/initializer.rb, in other words, how to extend it?

My requirement is to initialize ActionMailer::Base.smtp\_settings. Though  
there’s a built-in way to do it, i prefer the way performed as  
database.yml,  
which can put development, production and test environment all in a  
file.

For example:

development:  
server: 127.0.0.1  
port: 25  
domain: localhost

production:  
server: 127.0.0.1  
port: 25  
domain: localhost

Recently, I use this way.

module Rails  
class Initializer  
def initialize\_smtp  
if configuration.frameworks.include?(:action\_mailer) && RAILS\_ENV  
!=  
‘test’  
ActionMailer::Base.smtp\_settings =  
(configuration.smtp\_configuration[RAILS\_ENV]  
|| {}).symbolize\_keys  
end  
end  
end

class Configuration  
attr\_accessor :smtp\_configuration\_file

```
def smtp_configuration
  self.smtp_configuration_file ||= default_smtp_configuration_file
  YAML::load(ERB.new(IO.read(smtp_configuration_file)).result)
end

private
def default_smtp_configuration_file
  File.join("#{RAILS_ROOT}", "config", "mailer.yml")
end

```

end  
end

But following this, I only can initialize the mail system after the  
initialization, as:

initializer = Rails::Initializer.run do |config|  
end  
initializer.initialize\_smtp

It works, but not perfect really.

Does this a way to directly do the work when rails initialize itself?  
Thank  
you for any advice.

---

<div class="post-metadata">

**Author:** ![Ye\_Dingding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/49beb7/32.png) [@Ye\_Dingding](https://www.ruby-forum.com/u/Ye_Dingding)\
**Post date:** [September 9, 2007, 10:59am UTC](https://www.ruby-forum.com/t/how-to-add-a-initialize-method-when-rails-app-starts/113953/2 "2007-09-09T10:59:38Z")

</div>

Does anybody have any advice?

Or i have to rewrite process method to fit the requirement.

---

<div class="post-metadata">

**Author:** ![Ye\_Dingding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/49beb7/32.png) [@Ye\_Dingding](https://www.ruby-forum.com/u/Ye_Dingding)\
**Post date:** [September 9, 2007, 11:43am UTC](https://www.ruby-forum.com/t/how-to-add-a-initialize-method-when-rails-app-starts/113953/3 "2007-09-09T11:43:45Z")

</div>

How about solving it with “after\_initialize” callback?  
[http://pastie.caboo.se/95439](http://pastie.caboo.se/95439)
