Hi Rails, I have a string in my controller that I want to make into a model name. EG: Class LlamaController < ApplicationController def addsomething model = params[:model_name] # Here, I would like to take the string in model and use it as a Model @foo = Model.new end end Is this possible? Is there a better way? Thanks --Dean --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Cool, thanks. 0 urzatron app/models % ../../script/console Loading development environment.>> Class.const_get(''User'').new=> #<User:0xf7151cd0 @attributes={"created_on"=>nil, "updated_on"=>nil, "password"=>"", "login"=>"", "css_style"=>"default"}, @new_record=true> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
> I have a string in my controller that I want to make into a model name. > EG: > > Class LlamaController < ApplicationController > def addsomething > model = params[:model_name] > # Here, I would like to take the string in model and use it as a > Model > @foo = Model.new > end > end > > Is this possible? Is there a better way?@foo = Class.const_get(model).new I think. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Wednesday 25 October 2006 23:17, Dean wrote:> Hi Rails, > > I have a string in my controller that I want to make into a model > name. EG: > > Class LlamaController < ApplicationController > def addsomething > model = params[:model_name] > # Here, I would like to take the string in model and use it as a > Model > @foo = Model.new > end > end > > Is this possible? Is there a better way?model.constantize or model.camelize.constantize Inflector magic... Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---