Type casting

Hello,
I have a user table linked to the login code that was in the
“Agile” book. When someone signs in they are assigned to a group, such
as “adult” or “teenager”. I would like to take this group string and
reference a table with “find_by_sql()” so that:
(group.capitalize).find_by_sql() would work (and, of course, in the
select request in the parameter I would concatenate an “s” for the table
name: (select name from " + group + “s”) ).
An error is produced that the string “Adult” does not have a method
“find_by_sql”, so the code has not type cast the string to the model.
How do I call that model?
Thanks,
Barry

barry wrote:

How do I call that model?
Thanks,
Barry

group.capitalize.constantize.find_by_sql() should work, as should any of
the normal AR find methods, e.g.
group.capitalize.constantize.find(:all), etc

Thanks, Chris!
“constantize” worked. I didn’t know that command existed. It
allowed me to generalize the code so that future tables won’t require a
code change. Thanks, again!
Barry

Chris T wrote:

barry wrote:

How do I call that model?
Thanks,
Barry

group.capitalize.constantize.find_by_sql() should work, as should any of
the normal AR find methods, e.g.
group.capitalize.constantize.find(:all), etc

It’s not well documented (Saw Ezra mention it in a post a while back),
and there are other ways of doing it, but I like its conciseness, and
have used it in quite a few places.