More STI Tricks

ruby OOP STI

Wed Feb 10 13:35:41 -0800 2010

Obviously, I’ve been using STI for a recent project.

Alex Reisner has a post full of tips, a cool one being overriding the model_name class method for your parent class, so that when you do form_for @sub_class... you still get params[:parent_class] delivered to your controller.

def self.model_name
  name = "vehicle"
  name.instance_eval do
    def plural;   pluralize;   end
    def singular; singularize; end
  end
  return name
end

My God. It’s full of win.

blog comments powered by Disqus