Basic crud pages for STI models

If you have a model that is using single table inheritance, the simple new, create, edit, and update methods and views might not do what you want them to do. I have an ActiveRecord::Base with just a name and a type so I can define a Team (type) of "account" and a Dept of Education. But suppose I misspell Education and I want to edit it. What happens is the edit method finds the record and makes it into a Dept (type or class). This confuses the form_for(@name) into producing URLs for Dept which in my case isn't want I wanted. I wanted to edit the entry as a name. (In my case, I don't have URLs and controllers for Team or Dept, etc.)

The solution was to us becomes(klass) so the edit method becomes:

  def edit     @name = Name.find(params[:id]).becomes(Name)   end

I did that same thing after all of the calls to find **in this case**. There are probably 99% of the time that you want find to change the instance into the proper class but if you don't, this is a simple way to cast it back to the parent class.