Hello. I am trying to "soft-code" the model names in my app so I can use a generic method call to achieve some results. Many of my modules have the the following method: def ModelName.get_array # same code goes here for every module and it returns an array. end Is there a way to replace the model name with a variable or something else in such a way that the method can be called in a generic way? Something like: my_array = @model_name.get_array Thanks. Pepe --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Tue, Jul 29, 2008 at 7:49 AM, pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote:> > Hello. > > I am trying to "soft-code" the model names in my app so I can use a > generic method call to achieve some results. Many of my modules have > the the following method: > > def ModelName.get_array > # same code goes here for every module and it returns an array. > end > > Is there a way to replace the model name with a variable or something > else in such a way that the method can be called in a generic way? > Something like: > > my_array = @model_name.get_arrayTake a look at constantize: http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000488 You should be able to do something like this @model_name.constantize.get_array> Thanks. > > Pepe > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
This might be better written as class level methods then and mixed at the class level. Here''s an example of how that would work: module Foo def self.included(base) base.extend ClassLevelMethods end # You can still just define instance level methods as normal def bar # ... end # But now these methods will be mixed in at the class level module ClassLevelMethods def baz # ... end end end and just include Foo as normal. HTH RSL On Tue, Jul 29, 2008 at 9:17 AM, Christopher Kintner <kintner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Tue, Jul 29, 2008 at 7:49 AM, pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote: >> >> Hello. >> >> I am trying to "soft-code" the model names in my app so I can use a >> generic method call to achieve some results. Many of my modules have >> the the following method: >> >> def ModelName.get_array >> # same code goes here for every module and it returns an array. >> end >> >> Is there a way to replace the model name with a variable or something >> else in such a way that the method can be called in a generic way? >> Something like: >> >> my_array = @model_name.get_array > > Take a look at constantize: > http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000488 > > You should be able to do something like this @model_name.constantize.get_array > >> Thanks. >> >> Pepe >> > >> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the tip. I have tried ''constantize'' with variables and it worked like a charm. I''ll try it with my models and see how it works. Do you know how to do something similar with variables? I have a page with a table of 31 rows and all the field names follow the format ''field_namexx'' where ''xx'' is a sequence number. I have tried constantize with regular variables but it only works with... constants! Go figure! :) Thanks a lot. Pepe On Jul 29, 9:17 am, "Christopher Kintner" <kint...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Jul 29, 2008 at 7:49 AM, pepe <P...-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote: > > > Hello. > > > I am trying to "soft-code" the modelnamesin my app so I can use a > > generic method call to achieve some results. Many of my modules have > > the the following method: > > > def ModelName.get_array > > # same code goes here for every module and it returns an array. > > end > > > Is there a way to replace the model name with avariableor something > > else in such a way that the method can be called in a generic way? > > Something like: > > > my_array = @model_name.get_array > > Take a look at constantize:http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/Strin... > > You should be able to do something like this @model_name.constantize.get_array > > > Thanks. > > > Pepe--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Russell. I understand (or almost) what you have in mind. It would probably be the the ''right'' way to do it. I''ll play with it and see what I can come up with. Thanks a lot. Pepe On Jul 29, 11:28 am, "Russell Norris" <r...-ftMzyaTR+bHNyFkoKTPOtdi2O/JbrIOy@public.gmane.org> wrote:> This might be better written as class level methods then and mixed at > the class level. Here''s an example of how that would work: > > module Foo > def self.included(base) > base.extend ClassLevelMethods > end > > # You can still just define instance level methods as normal > def bar > # ... > end > > # But now these methods will be mixed in at the class level > module ClassLevelMethods > def baz > # ... > end > end > end > > and just include Foo as normal. HTH > > RSL > > On Tue, Jul 29, 2008 at 9:17 AM, Christopher Kintner <kint...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Tue, Jul 29, 2008 at 7:49 AM, pepe <P...-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote: > > >> Hello. > > >> I am trying to "soft-code" the modelnamesin my app so I can use a > >> generic method call to achieve some results. Many of my modules have > >> the the following method: > > >> def ModelName.get_array > >> # same code goes here for every module and it returns an array. > >> end > > >> Is there a way to replace the model name with avariableor something > >> else in such a way that the method can be called in a generic way? > >> Something like: > > >> my_array = @model_name.get_array > > > Take a look at constantize: > >http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/Strin... > > > You should be able to do something like this @model_name.constantize.get_array > > >> Thanks. > > >> Pepe--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---