table_name, field_name = ''people.name''.split(/\./)
ActiveRecord::Base.const_get(
table_name.classify
).find(:all).collect{|item|
[item.send(field_name), item.id]
}
On Fri, 2005-10-14 at 15:33 -0400, Luke Galea wrote:> Hi all,
>
> The short of my question is: Given a string in that represents the
> table name, how do I reflectively/dynamically get access to the AR
> Model class that represent it? (ie: from ''people'' get the
class
> Person that extends ActiveRecord::Base)??
>
> I''ve integrated the datavision report writer in with my rails app.
> I''m trying to use rails to gather the parameters needed to run the
> report and in the case of associations I want to give the user a drop
> down to select the parameter value.
>
> so I have "people.name" as the parameter in my report file..
>
> The first part is the hard bit.. How do I say "What is the AR model
> that represents the table ''people''?" ??
>
> If I had that, it would be simple to do the rest.. ModelClass.find
> (:all).collect { |item| [ item.send( field ), item.id ] }
>
> Failing this I could switch to "Person.name" for the parameter..
but
> even then.. How do I grab the class based on the string
''Person''??
> eval ''Person'' would work but doesn''t feel right
:(
>
> Thanks in advance!
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails