Forum: Ruby on Rails Organisation of code in rails app

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
873a9db33e289b558e89c039629562f5?d=identicon&s=25 James Smith (jmdjmsmith)
on 2007-01-28 12:37

When my rails application starts i would like to initialize some objects
using data retrieved from the database.

The classes for these objects are currently stored in the 'helpers'
directory, is this correct?

The method that i use to create these objects is my
appication_controller_helper, is this where it should be?

Where abouts should i be calling this initialize method to setup the
objects? I understand that you shouldn't call helper methods from the

Any pointers on these issues would be very much appreciated.
Thank you
419c9cc258895bed66c89892721c2971?d=identicon&s=25 Elliott Blatt (eblatt)
on 2007-01-29 03:19

I believe that you want to put all you initialization stuff in an 'init'
method inside of application.rb.  I don't believe that helpers are the
best choice here.

Try something like this:

class ApplicationController < ActionController::Base
  before_filter :init

  def init
      @tags = Tag.tags(:limit => 200, :order => "name asc")


Elliott Blatt
C4fd1555c076437dffdfd33a14679246?d=identicon&s=25 Amir Javs (amirj26)
on 2007-01-31 06:09
(Received via mailing list)
I was in a similar situation as well, and what Elliott has suggested
will work. However, you have to realize that the initialization of
that @tags variable will happen for every single call made to any
method belonging to any of the controllers. This is because the
controllers inherit from ApplicationController, and that filter is run
everytime before any controller method runs, so you'd effectively be
doing your initialization of stuff not only when your rails
application starts, but also each time a controller's method is

I'm not sure what the best way to avoid having this happen is, but
what I've done is created another class (say 'ClassFoo') containing
the information I want to have initialized once, with this info being
contained in class variables, and accessed through class methods. In
the above init before_filter, I then make a call into some function
'foo' which is a class method of 'ClassFoo', which performs all the
initialization. However since this class is not an actual controller,
it's class variables can be made to be initialized only once (by
having some boolean class variable to check whether I've initialized
yet). So you're still going to end up calling 'foo' on each controller
method invocation, however the initialization can be controlled to
occur only once.

Hopefully that made some sense. I'm not claiming its the nicest way by
any means, but I couldn't find any other way.



On Jan 28, 6:19 pm, Elliott Blatt <>
This topic is locked and can not be replied to.