views:

40

answers:

1

Hi all,

I have a class method mixed in to all my models. the method gets called when the model class is evaluated. unfortunately (for me), this seems to be on-demand, whenever the model is needed in development env. how can have rails load all the models at start up? is this even advisable?

class Foo < ActiveRecord::Base
  include Acl
  register_acl # i need this to be called for all models at start up
end

Basically, the register_acl takes a few arguments of "actions" that the model would like to be access controlled. Suppose one of the action of Foo is "manage" and the system needs to be aware of this action at start up. I think in the model is the most natural place to have this logic.

thank you!

A: 

In MVC concept models are not intended to act by themselves, i.e. they should only act when controller sends them a message (for example, @foo.register_acl). Model instances even should not exist until they are created by controller.

What are you trying to achieve with your register_acl method?

If you really need something to be executed on object creation you can use initialize() method which is called whenever a Ruby object is created.

However if you need model to execute some code by itself you are most likely facing some code smell and you need to change something within your app.

Slobodan Kovacevic
Thanks for the response! I've updated my question to let you know what i am trying to do. I am not trying to create/get instances of the model. I just need the model file to be loaded and evaluated by Rails at startup. In a regular ruby program, you can expect that class code to be evaluated at startup.Thanks!
janechii