On Sep 2, 2006, at 1:19 PM, Paul Duncan wrote:

> * J2M (james2mccarthy / gmail.com) wrote:
>>
>> Jan Svitok wrote:
>>
>>> model = const_get(model_name.to_s.capitalize)
>
> This might be more appropriate:
>
>   model_class = const_get(model_name.to_s.classify)

Also if you are already within rails you may as well use #constantize

model_class = model_name.to_s.classify.constantize

-Ezra

>
> Classify will take care of things that capitalize misses, like
> underscores and extraneous plurals.  Take a look at the following
> output:
>
>   # test strings and test methods
>   strings = %w{blog_post layers magazine_subscribers}
>   methods = %w{capitalize classify}.map { |m| m.intern }
>
>   # iterate over test strings and handle each one
>   puts strings.map { |str|
>     "'#{str}'\n" << methods.map { |meth|
>       "  #{meth}:\t'#{str.send(meth)}'"
>     }.join("\n")
>   }
>
> Produces the following:
>
>   'blog_post'
>     capitalize:   'Blog_post'
>     classify:     'BlogPost'
>   'layers'
>     capitalize:   'Layers'
>     classify:     'Layer'
>   'magazine_subscribers'
>     capitalize:   'Magazine_subscribers'
>     classify:     'MagazineSubscriber'
>
> Hope that helps...
>
> -- 
> Paul Duncan <pabs / pablotron.org>        pabs in #ruby-lang (OPN IRC)
> http://www.pablotron.org/               OpenPGP Key ID: 0x82C29562