Is there a way to access an activerecord model programmatically via a string of it''s name? Specifically, i''m getting *args from a <%= text_field %> call, which includes a hash of the table/field strings so I can set the maxlength of the textbox ... def stext_field(*args) # need to get the model associated with args[0] limit = Model.content_columns[i].limit args << {:maxlength => limit} text_field(args) end But is it possible to access a model via the string name of the table? Britt
Britt Selvitelle wrote:> Is there a way to access an activerecord model programmatically via a > string of it''s name? > > Specifically, i''m getting *args from a <%= text_field %> call, which > includes a hash of the table/field strings so I can set the maxlength > of the textbox ... > > def stext_field(*args) > # need to get the model associated with args[0] > limit = Model.content_columns[i].limit > args << {:maxlength => limit} > text_field(args) > end > > But is it possible to access a model via the string name of the table? >eval() is pretty slow, so you wouldn''t want to do a jillion of these. If you find this to be a limiting factor, you could do something crazy like prepopulate a hash with the various model Class objects as the values, and the names as the keys. That being said, this should work: (Split into multiple statements for clarity, assumes that the table name is capitalized, e.g. "Model") tname = args["table_name"] eval("limit = #{tname}.content_columns[#{i}].limit")
On 8/18/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote:> > But is it possible to access a model via the string name of the table? > > > eval() is pretty slow, so you wouldn''t want to do a jillion of these. If you > find this to be a limiting factor, you could do something crazy like prepopulate > a hash with the various model Class objects as the values, and the names as the > keys. ><snipped> You can use Object.const_get(class_name) to get a reference to a class. e.g. Object.const_get(class_name).content_columns[i].limit Chris
Chris McGrath wrote:> On 8/18/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote: > >>>But is it possible to access a model via the string name of the table? >>> >> >>eval() is pretty slow, so you wouldn''t want to do a jillion of these. If you >>find this to be a limiting factor, you could do something crazy like prepopulate >>a hash with the various model Class objects as the values, and the names as the >>keys. >> > > <snipped> > > You can use Object.const_get(class_name) to get a reference to a class. > e.g. Object.const_get(class_name).content_columns[i].limit >That''s a new one to me. Very nice. Presumably you could also do @instance = Object.const_get(class_name).new --Wilson.
On 8/18/05, Chris McGrath <c.r.mcgrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/18/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote: > > > But is it possible to access a model via the string name of the table?constantize is another option. The nice thing about constantize is it lets you do this: ''Module1::Module2::MyClass''.constantize. If you''re starting off from a pluralized table name, you can do this: ''posts''.classify.constantize.find..... Here are other rails string inflector methods: http://rails.rubyonrails.com/classes/Inflector.html -- rick http://techno-weenie.net
>> >> But is it possible to access a model via the string name of the table? >>Kernel.const_get "ClassName" will get you a reference to the class.
On 8/18/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/18/05, Chris McGrath <c.r.mcgrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 8/18/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote: > > > > But is it possible to access a model via the string name of the table? > > constantize is another option. The nice thing about constantize is it > lets you do this: > > ''Module1::Module2::MyClass''.constantize. > > If you''re starting off from a pluralized table name, you can do this: > > ''posts''.classify.constantize.find..... > > Here are other rails string inflector methods: > http://rails.rubyonrails.com/classes/Inflector.htmlAmazing! Britt