How can I put multiple model classes to one file?

It seems, by default, the model_name.rb file is a must for a model.
I just created a ‘bars’ table in database, and I can’t access Bar model
until I create an app/models/bar.rb file with:

class Bar < ActiveRecord::Base
end
even moving these two line codes to app/models/foo.rb raise error.

I want a Bar model, without a bar.rb file. I want to do this because I
want to have a SUPER model class to GENERATE models.
While tables are ready, I want to use methods like:

def add_model(model_name)
#define model_name as a new model class
end

Any idea how to do this?

Nanyang Z. wrote:

It seems, by default, the model_name.rb file is a must for a model.
I just created a ‘bars’ table in database, and I can’t access Bar model
until I create an app/models/bar.rb file with:

class Bar < ActiveRecord::Base
end
even moving these two line codes to app/models/foo.rb raise error.

I want a Bar model, without a bar.rb file. I want to do this because I
want to have a SUPER model class to GENERATE models.
While tables are ready, I want to use methods like:

def add_model(model_name)
#define model_name as a new model class
end

Any idea how to do this?

The Rails auto-loader code assumes that a class named ClassName is found
in a source file class_name.rb in one of a number of directories. You
can work around that by explicitly requiring the source file, which will
define the class, meaning the auto-loader will never have to look for
the class. If you want things to load automatically, you’l have to hack
the dependency/auto-loader code. I wouldn’t advise that, as that code
is some of the hariest in all of Rails.

I guess I don’t understand why your class generator needs to shove all
the classes into a single file. Can’t you generate each class in its own
file?


Josh S.
http://blog.hasmanythrough.com

Hi Nanyang,
even i have the same question “why your class generator needs to shove
all the classes into a single file?”

keeping aside that question…!!

(I don’t know whether this is answering your problem or not?)
just give a try like this…

module ModelGenerator
def add_model(model_name, table_name)
dynamic_model = “class #{model_name} < ActiveRecord::Base ;
set_table_name ‘#{table_name}’; end;”
eval(dynamic_model)
end
end

Now you can say something like this,

require ‘model_generator’
include ModelGenerator

add_model(‘Person’, ‘people’)
Person.find(10) # what-ever code you want.

On Sep 7, 4:49 pm, Nanyang Z. [email protected]
wrote:

def add_model(model_name)
#define model_name as a new model class
end

A
ny idea how to do this?

Josh S. wrote:

If you want things to load automatically, you’l have to hack
the dependency/auto-loader code. I wouldn’t advise that, as that code
is some of the hariest in all of Rails.

I‘ve work around by making rails generate and writing these model rb
files to the app/models dir, and it works fine.

I guess I don’t understand why your class generator needs to shove all
the classes into a single file. Can’t you generate each class in its own
file?

By putting all classes into a single file, I think I can control the
content of these class more easily, since most of these classes will
have similar methods, which maybe I can generate with dynamically.

Now generate each class file is not a bad option. After all, I can
generate these files.

raghukumar wrote:

(I don’t know whether this is answering your problem or not?)
just give a try like this…

Thanks, raghukumar, I’ll try it!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs