Hi, I wonder if there''s an easy way to access the associated Model class from its Controller. Something that would allow me to write snippets like the following: class PeopleController < ApplicationController def some_method person_model_class = self.some_method_to_retrieve_the_person_model_class() end end Cheers, Marco -- Posted via http://www.ruby-forum.com/.
Marco Lazzeri wrote:> Hi, > > I wonder if there''s an easy way to access the associated Model class > from its Controller. Something that would allow me to write snippets > like the following: > > class PeopleController < ApplicationController > def some_method > person_model_class = > self.some_method_to_retrieve_the_person_model_class() > end > end > > Cheers, > Marco > >Hello, Had the same problem here... Controllers aren''t associated to one specific model. But yes, for general CRUD operations, you''ll usually just use one... So, this will probably do what you want: model = Object.const_get controller_name.camelcase The benefit is that you can have PeopleController < GenericController, and define generic CRUD things in the latter. Have a lot of fun, -- ---------------------------------------------------------------------- Yannick Majoros http://www.inma.ucl.ac.be/~majoros Informaticien UCL/INMA-MEMA 4, avenue G. Lema?tre B-1348 Louvain-la-Neuve Tel: +32-10-47.80.10 Fax: +32-10-47.21.80 ---------------------------------------------------------------------- Mon calendrier en ligne : http://www.inma.ucl.ac.be/~majoros/calendar Accents bizarres ? http://www.inma.ucl.ac.be/~majoros/email.html ---------------------------------------------------------------------- -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 3403 bytes Desc: S/MIME Cryptographic Signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060714/e055e1cb/smime.bin
Although common practice to create one controller per model, there''s nothing to say there should be a one-to-one or indeed any relationship between models and controllers. One controller may deal with many models, one model may be used in many controllers. I''m guessing you want to do this to reuse code in some way. If so, perhaps if you explain which code you want to reuse the list can help come up with an appropriate strategy (abstract class, module, etc). Tom On 7/14/06, Marco Lazzeri <marco.lazzeri@gmail.com> wrote:> Hi, > > I wonder if there''s an easy way to access the associated Model class > from its Controller. Something that would allow me to write snippets > like the following: > > class PeopleController < ApplicationController > def some_method > person_model_class > self.some_method_to_retrieve_the_person_model_class() > end > end > > Cheers, > Marco > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 7/14/06, Yannick Majoros <majoros@inma.ucl.ac.be> wrote:> So, this will probably do what you want: > > model = Object.const_get controller_name.camelcase >You''ll probably need to singularize the name as well: model = Object.const_get controller_name.singularize.camelcase Tom