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
--
Posted via http://www.ruby-forum.com/.
barry wrote:> 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 > >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
--
Posted via http://www.ruby-forum.com/.
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. barry wrote:> 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 >> > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060703/16d59db2/attachment.html