Ruby on Rails: String to Object
In my new app I have a whole bunch of nested models. For the most part they all have the same database structure and can use very similar partials. I hate to repeat code because it usually makes my life more difficult when I need to go back and change something or do maintenance. I am now placing these partials in a single location that all of these models have access to.
Problem is I had a very difficult time telling the partials what object it needed to represent without adding a parameter. (I am making a lot of AJAX calls) I solved it by using the controller that was passed along with the request. After that I needed to take that string that was passed and transform it so ruby recognized it as an object. This was my final solution:
Classify takes the string and gives it the proper camelcase format so that it conforms to the model naming convention. Constantize then takes that string and converts it into an actual object.