# Adding a new class

**URL:** <https://www.ruby-forum.com/t/adding-a-new-class/132669>\
**Category:** Rails\
**Created:** [March 22, 2008, 10:26pm UTC](https://www.ruby-forum.com/t/adding-a-new-class/132669 "2008-03-22T22:26:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 22, 2008, 10:26pm UTC](https://www.ruby-forum.com/t/adding-a-new-class/132669/1 "2008-03-22T22:26:03Z")

</div>

If I want to add a class so I can create instances of that class in a  
controller, where do I put the code to create the class?

There. I said it. It’s embarrassing to admit that I don’t know the  
answer to that question and it illustrates my poor understanding of  
the overall RoR framework structure. So, if anyone can also point me  
to something I could read that would enlighten me on the overall RoR  
framework structure, I would be appreciative of that as well.

Thanks for any input.

```
     ... doug
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 22, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/adding-a-new-class/132669/2 "2008-03-22T22:49:54Z")

</div>

doug wrote:

> If I want to add a class so I can create instances of that class in a  
> controller, where do I put the code to create the class?

If the class is not an ActiveRecord model, then place it in a file in  
the application’s “lib” directory. The filename should be the  
lower-cased version of the class name with words separated by  
underscores (MyClass =\> lib/my\_class.rb). When you access “MyClass”,  
Rails will automatically know how to find the definition and load it in.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 22, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/adding-a-new-class/132669/3 "2008-03-22T22:53:14Z")

</div>

I’m not entirely sure I’ve understood the question, but if you class  
is called Foo, then it should live in app/models/foo.rb  
You can then call Foo.create or Foo.new from anywhere you want. If you  
want your own methods for creating new instance you could write

class Foo \< ActiveRecord::Base  
def self.make\_me\_a\_new\_one  
…  
end  
end

and then call Foo.make\_me\_a\_new\_one from anywhere you want.

Fred

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 22, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/adding-a-new-class/132669/4 "2008-03-22T23:12:42Z")

</div>

That works, but I prefer to separate models and new classes.

I usually put extra classes in “extras/” and then uncomment the  
following in environment.rb:

# Add additional load paths for your own custom dirs

config.load\_paths += %W( #{RAILS\_ROOT}/extras )

That way my new classes and the database-derived models are not  
intermixed. It’s up to you, really. You can use lib, or extras, so  
long as rails can see it and your project makes sense to you and your  
team.

-john
