29 Dec 2011, 17:23
Generic-user-small

Mahesh Dsouza (2 posts)

I had a question on Reflection. Assuming that I have an ActiveRecord object (Post). I know as an object we can do Post.find_by , Post.where . Can we do something like s= “Post” s.find_by s.where

01 Jan 2012, 03:00
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

What is the class of “s”? That class doesn’t support the #where method.

04 Jan 2012, 01:02
Generic-user-small

Mahesh Dsouza (2 posts)

Actually I should have elaborated more. Lets say I have a page which takes the name of a Model. Example -> Post . On submit I need to get all the columns associated with the Model object. So I need to somehow dynamically find the value of “Post” which is a string and dynamically translate it to a Model object. I am from a java background and new to ruby. In java you can use reflection to create such dynamic objects passing string. I tried using model_obj = eval(“Post”) but somehow I get an error “NoMethodError (undefined method `coulmns’ for #):". Incidentally I do the same thing using Rails console and it works. It just doesn't work in the application. Please let me know if I am missing anything and also I want to thank you for the great screen cast it helped me a lot understanding ruby more.

01 Feb 2012, 14:06
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

If that’s the actual error message, then it;s simply that “columns” was misspelled.

  You must be logged in to comment