Topic: Using code multiple times in multiple controllers.

Ive got a piece of code, which is  retrieving data from activerecord. And i use this piece of code muttiple times at multiple controllers / views. What is the best way to use this as a fuction and call it from every controller.

I tried already put in application controller but no result.

It have to return some more date than only one return value.

So whats the best way to do this ?

Re: Using code multiple times in multiple controllers.

If you only need it in some models, you should make a new file in the lib folder of your rails app, then you just require it like so at the top of your model.

require 'your_file.rb'

If you need this across all models, you may need to require this file in your environment.rb

Re: Using code multiple times in multiple controllers.

Can you post the code? It's hard to see where it should go without knowing what it's doing.

Railscasts - Free Ruby on Rails Screencasts