# Accessing a class in Model from Controller

**URL:** <https://www.ruby-forum.com/t/accessing-a-class-in-model-from-controller/142756>\
**Category:** Rails\
**Created:** [July 11, 2008, 3:36am UTC](https://www.ruby-forum.com/t/accessing-a-class-in-model-from-controller/142756 "2008-07-11T03:36:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 11, 2008, 3:36am UTC](https://www.ruby-forum.com/t/accessing-a-class-in-model-from-controller/142756/1 "2008-07-11T03:36:16Z")

</div>

I have a model with Class Cart, but when I try to Cart.new in my  
controller, it does not recognize the constant Cart. Suggestions??

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 11, 2008, 5:17am UTC](https://www.ruby-forum.com/t/accessing-a-class-in-model-from-controller/142756/2 "2008-07-11T05:17:23Z")

</div>

Hi –

On Fri, 11 Jul 2008, Justin To wrote:

> I have a model with Class Cart, but when I try to Cart.new in my  
> controller, it does not recognize the constant Cart. Suggestions??

Please don’t keep asking the same question, especially multiple times  
per day.

When you refer to Cart in the controller, Rails will look in its load  
path for a file called cart.rb, and if it finds one, it will load it,  
on the expectation that Cart will be defined there. Are you sure your  
model file is called cart.rb, and that it’s in the app/models  
directory?

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 11, 2008, 5:39am UTC](https://www.ruby-forum.com/t/accessing-a-class-in-model-from-controller/142756/3 "2008-07-11T05:39:16Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Fri, 11 Jul 2008, Justin To wrote:
> 
> > I have a model with Class Cart, but when I try to Cart.new in my  
> > controller, it does not recognize the constant Cart. Suggestions??
> 
> Please don’t keep asking the same question, especially multiple times  
> per day.

Sorry for the duplicate post. Thought maybe a more descriptive title  
would yield results.

I did manage to figure the problem out earlier, and it was the file  
name. I’m unfamiliar with the rails conventions, as I am still learning.

Thanks

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 11, 2008, 5:57am UTC](https://www.ruby-forum.com/t/accessing-a-class-in-model-from-controller/142756/4 "2008-07-11T05:57:45Z")

</div>

Hi –

On Fri, 11 Jul 2008, Justin To wrote:

> > Please don’t keep asking the same question, especially multiple times  
> > per day.
> 
> Sorry for the duplicate post. Thought maybe a more descriptive title  
> would yield results.
> 
> I did manage to figure the problem out earlier, and it was the file  
> name. I’m unfamiliar with the rails conventions, as I am still learning.

For most setup and housekeeping tasks, Rails helps you with either  
rake tasks or admin scripts. To create a model, you would do this,  
from the top directory of your application:

ruby ./script/generate model cart

and the correctly-named file will be created for you. It’s good to  
understand why things do or do not work, but it’s also good to let  
Rails do these automated tasks, since it does them consistently and  
quickly.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
