Adding a new class

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

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.

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

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