4
覆盖ActiveRecord属性方法
我在说的一个例子: class Person < ActiveRecord::Base def name=(name) super(name.capitalize) end def name super().downcase # not sure why you'd do this; this is just an example end end 这似乎可行,但是我刚刚阅读了ActiveRecord :: Base docs中有关覆盖属性方法的部分,并建议使用read_attributeand write_attribute方法。我认为以上示例中的操作一定存在问题;否则,为什么他们会认为这些方法是覆盖属性方法的“正确方法”?他们也强迫使用更丑陋的习惯用法,因此一定有充分的理由... 我真正的问题:这个例子有什么问题吗?