code layout question

I'm trying to figure out where to put objects that aren't strictly
"model" objects (ie, not database/business objects, these are actually
objects holding information regarding the ui). I've seen a couple
answers which is to create the classes in a module file in /lib, or to
create the objects as non-ActiveRecord class files.

Both of these solutions work, of course. However, when working in the
rails development environment they aren't reloaded on the fly in
webrick/mongrel when they're modified. Restarting the server everytime
I modify one is a bit of a pain.

The one fix I have found is to just throw the class definitions in one
of the Helper files and instantiate it with the full path, with an eye
to moving it to lib when finished. It works, but it's really nasty and
I would prefer to come up with a "right" solution.

The concept would be, for instance, that I am creating a web
application that has multiple windows. Each window is an instantiation
of a Frame object, it's the Frame objects I need to know where to put.

Thanks for any help!


Put them in /lib.

To make the classes reload automatically:

class YourLibClass
include Reloadable #pretty sure that’s the name



Leave them in /lib and put this at the top of your class...

class MyClass
    include Reloadable

That should solve the reloading issue in development. wrote:

  include Reloadable #pretty sure that's the name

That's it, however sometimes Rails is weird about when it'll reload and
when it won't. I still have to restart webrick/mongrel in a few cases
(which I can't remember at the moment). Anyone care to enumerate them

Hrm, I try that, and restart the server. First request is fine, hit
reload (change or no change to class) returns:

NameError in TestController#index

uninitialized constant MyClass

Here's the module file currently in /lib

module MyLib
  class MyClass
    include Reloadable

    def doit
      puts "This work?"

probably seeing what eden li was talking about. I forgot to include
the code in the controller that calls it. . .

x =


Hardy wrote:

Can you post the code to TestController#index?


class TestController < ApplicationController
  def index
    puts "in index"
    x =

See anything? *hopes*

Jason Roelofs wrote:

Umm, please change "Duh" to "MyClass" ; )

Cleaning up obnoxious code for public presentation.

Hardy wrote:

I think I see what’s going on here. The lib/ system is very dependent on the filename <-> classname relationship. Thus, you have lib/my_lib.rb, Rails is going to look for class MyLib. Otherwise, you have to manually require the file when you want to use it.

So in your case, try moving MyClass out of the module, and make sure that the file is named lib/my_class.rb. That should take care of this problem.


That did it!! Kind of strange it didn't like it from the module, but
it's not a problem for the application to just define a class file.

Thank you all very much!!


The lib/ system is very dependent on the filename <-> classname relationship.

Aha! That solves my problem too. Is this documented somewhere?
Things outside of app/, config/ (except routes.rb) and public/ all seem
magical to me.