Skip to content

Instantly share code, notes, and snippets.

@schovi
Created November 25, 2010 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schovi/715801 to your computer and use it in GitHub Desktop.
Save schovi/715801 to your computer and use it in GitHub Desktop.
How to mass assign attribute 'type' for STI with validation
class Order < ActiveRecord::Base
validates_inclusion_of :type, :in => %w{SpecialOrder}
def self.new(params = {})
_type = params.delete(:type)
_new = super(params)
_new.type = _type.to_s.camelize if _type
return _new
end
end
class SpecialOrder < Order
end
order = Order.new :type => "special_order", :other_attribute => ....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment