# Where do i put classes that i want to use in Controller?

**URL:** <https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656>\
**Category:** Rails\
**Created:** [January 28, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656 "2007-01-28T14:36:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [January 28, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656/1 "2007-01-28T14:36:22Z")

</div>

Hello,

Can someone please help me…

I would like to know where i should put classes that i need to use  
within my controller?

And how are they accessible to the controller?

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [January 28, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656/2 "2007-01-28T15:29:01Z")

</div>

James S. wrote:

> Hello,
> 
> Can someone please help me…
> 
> I would like to know where i should put classes that i need to use  
> within my controller?
> 
> And how are they accessible to the controller?

If it is a controller class, I’d put it in your app/controllers  
directory. Model classes in app/models, etc.

app/contollers/your\_controller.rb

def YourController \< ActiveConroller  
ModelClass.method()  
end

app/models/model\_class.rb

def ModelClass \< ActiveRecord::Base  
def method  
end  
end

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [January 28, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656/3 "2007-01-28T17:59:36Z")

</div>

Elliott B. wrote:

> If it is a controller class, I’d put it in your app/controllers  
> directory. Model classes in app/models, etc.

It is neither… Its just an additional class that i need to create  
objects for use within my controller, any ideas??

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [January 28, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656/4 "2007-01-28T18:09:10Z")

</div>

> Its just an additional class that i need to create  
> objects for use within my controller, any ideas??  
> If you create a class and put it in lib/, you’ll be able to access it  
> from anywhere in your application - including controllers.

Just name the file after the class name i.e. MyClass should be in lib/  
my\_class.rb. If you’re using 1.2, rails will include the file  
automatically. If you’re still on an earlier version, you’ll need to  
add a require line in yourself in environment.rb (require ‘my\_class’).

Hope that helps,

Steve

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [January 28, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656/5 "2007-01-28T18:08:10Z")

</div>

Hi –

On Sun, 28 Jan 2007, James S. wrote:

> Elliott B. wrote:
> 
> > If it is a controller class, I’d put it in your app/controllers  
> > directory. Model classes in app/models, etc.
> 
> It is neither… Its just an additional class that i need to create  
> objects for use within my controller, any ideas??

It sounds like a candidate for a file in lib/ .

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [January 28, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656/6 "2007-01-28T18:27:31Z")

</div>

> Hope that helps,

Yes, thanks very much.

One last question, if thats OK? When my application starts i need to do  
some basic setup work. This just involves calling a method that creates  
some objects, that are stored in the @session, based on data retrieved  
from the database. Can you advise me as to where this method should be  
called from (just the index of the first controller??) and where this  
‘setup’ method should be within my rails directory structure?

Thanks again

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [January 28, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/where-do-i-put-classes-that-i-want-to-use-in-controller/83656/7 "2007-01-28T22:33:04Z")

</div>

> where this  
> ‘setup’ method should be within my rails directory structure?  
> What I would do is add a before\_filter to your ApplicationController:

class ApplicationController \< ActionController:Base  
before\_filter :setup

private  
def setup  
if !session[:my\_variable]  
# get data and edit session  
end  
end  
end

Just to note, you should use the ‘session’ method to access the  
session data rather than the instance variable ‘@session’. You should  
also consider whether or not you really need to store objects in the  
session as it can cause issues. It’s best to keep the session small  
if you can.

Hope that helps,

Steve
