Good morning, I''m trying to dynamically define models. This method #1 works: model_name = User @Model = model_name @Model.find(params[:id]).name This method #2 doesn''t: model_name = "User" @Model = model_name @Model.find(params[:id]).name Anyone know how I can get method #2 to work? Is there something I can tack onto the end of ''model_name'' to get it to know that it''s a model? Thanks, Frank -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Try the eval method. For instance: classname = "Date" => "Date" eval(classname).today => Fri, 05 Feb 2010 -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frank_in_Tennessee wrote:> Good morning, > > I''m trying to dynamically define models. > > This method #1 works: > model_name = User > @Model = model_name > @Model.find(params[:id]).name > > This method #2 doesn''t: > model_name = "User" > @Model = model_name > @Model.find(params[:id]).name >Of course not. In method 2, @Model is a String, not a Class.> Anyone know how I can get method #2 to work? Is there something I can > tack onto the end of ''model_name'' to get it to know that it''s a model?Look up the constantize method.> > Thanks, > > FrankBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 5, 2010, at 11:16 AM, Sharagoz -- wrote:> Try the eval method. > > For instance: > > classname = "Date" > => "Date" > eval(classname).today > => Fri, 05 Feb 2010 >With ActiveSupport you get constantize. irb> constantize("Date") => Date You can either look at the ActiveSupport implementation or this one: # from Jim Weirich (based on email correspondence), improved by Rick Denatale (in ruby-talk:332670) def constantize(camel_cased_word) camel_cased_word. sub(/^::/,''''). split("::"). inject(Object) { |scope, name| scope.const_defined?(name) ? scope.const_get(name) : scope.const_missing(name) } end -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Works great now. Thank you. Modified Code: model_name = "User" @Model = model_name.constantize @Model.find(params[:id]).name On Feb 5, 10:32 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frank_in_Tennessee wrote: > > Good morning, > > > I''m trying to dynamically define models. > > > This method #1 works: > > model_name = User > > @Model = model_name > > @Model.find(params[:id]).name > > > This method #2 doesn''t: > > model_name = "User" > > @Model = model_name > > @Model.find(params[:id]).name > > Of course not. In method 2, @Model is a String, not a Class. > > > Anyone know how I can get method #2 to work? Is there something I can > > tack onto the end of ''model_name'' to get it to know that it''s a model? > > Look up the constantize method. > > > > > Thanks, > > > Frank > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frank_in_Tennessee wrote:> Works great now. Thank you. > > Modified Code: > > model_name = "User" > @Model = model_name.constantize > @Model.find(params[:id]).nameGreat. One other thing: @Model is unusual. You might consider @model instead as being more idiomatic Ruby. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.