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.
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
A
ny idea how to do this?