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
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?
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.