Organisation of code in rails app

Hello,

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
controller??

Any pointers on these issues would be very much appreciated.
Thank you

James,

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”)
end

end

Elliott B.
www.elliottblatt.com

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
invoked.

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.

Cheers,

Amir

On Jan 28, 6:19 pm, Elliott B. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs