26
如何在ActiveRecord中设置默认值?
如何在ActiveRecord中设置默认值? 我从Pratik看到一篇帖子,描述了一个丑陋,复杂的代码块:http : //m.onkey.org/2007/7/24/how-to-set-default-values-in-your-model class Item < ActiveRecord::Base def initialize_with_defaults(attrs = nil, &block) initialize_without_defaults(attrs) do setter = lambda { |key, value| self.send("#{key.to_s}=", value) unless !attrs.nil? && attrs.keys.map(&:to_s).include?(key.to_s) } setter.call('scheduler_type', 'hotseat') yield self if block_given? end end alias_method_chain :initialize, :defaults end 我看过以下示例: def initialize super self.status = ACTIVE unless self.status end …