Adding classes without script\generate


#1

Can I add classes to a rails application manually i.e without using
script generate Model/Controller?

The reason I am asking, I added a class by putting it in the controller
class. I can access the class and its method from command-line (irb),
but when I try to instantiate it in another controller, I get
“uninitialized constant” error.

If I try using script\generate Model ClassName, it adds “<
ActiveRecord::Base”, where as I need to inherit another class.

ClassName < ActiveRecord::Base

Thanks,

Pradeep


#2

Pradeep,

  1. If this class is meant to be a model then it should inherit from
    ActiveRecord::Base.

  2. I hand write classes all the time, there is no need to use the model
    generator if you don’t want to.

  3. You most probably need a require statement in your controller file.
    i.e. require ‘class_name’

  4. If it’s a model, you will have to put a “model
    :model_name_in_singular_lower_case” in your controller that accesses it

  5. You are getting the constant not recognized error since the
    interpreter can’t resolve your class.

I hope this helps and please be advised that I am not an expert in this
area.

ilan

Pradeep S. wrote:

Can I add classes to a rails application manually i.e without using
script generate Model/Controller?

The reason I am asking, I added a class by putting it in the controller
class. I can access the class and its method from command-line (irb),
but when I try to instantiate it in another controller, I get
“uninitialized constant” error.

If I try using script\generate Model ClassName, it adds “<
ActiveRecord::Base”, where as I need to inherit another class.

ClassName < ActiveRecord::Base

Thanks,

Pradeep


#3

I figured out the error.

I needed to restart my server every time, I make a change to the class
file (because it is not a controller and/or Model…)

feel like stupi… :slight_smile:

ilan berci wrote:

whoops… I meant require ‘file name’… damn my fingers… :slight_smile:

  1. You most probably need a require statement in your controller file.
    i.e. require ‘class_name’

#4

whoops… I meant require ‘file name’… damn my fingers… :slight_smile:

  1. You most probably need a require statement in your controller file.
    i.e. require ‘class_name’

#5

Are you using just regular irb or script/console? You need to use
script/console or else your class won’t be available.


#6

2006/5/2, Pradeep S. removed_email_address@domain.invalid:

I figured out the error.

I needed to restart my server every time, I make a change to the class
file (because it is not a controller and/or Model…)

feel like stupi… :slight_smile:

In 1.1, you can add “include Reloadable” in the model so that you
don’t have to restart the whole server.

Douglas