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 >endeven 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 >endAny idea how to do this? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nanyang Zhan 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Zhan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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?> -- > Po sted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Josh Susser 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---