Accessing a class in Model from Controller

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??

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 for details and updates!

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

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 for details and updates!