Rails ActiveRecord: Locking down attributes when record enters a particular state
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Wondering if there’s a plugin or best way of setting up an ActiveRecord class so that, for example, when a record enter the "published" state, certain attributes are frozen so that they could not be tampered with. 回答1: You can freeze an entire AR::B object by setting @readonly to true (in a method), but that will lock out all attributes. The way I would recommend is by defining attribute setter methods that check for the current state before passing to super: class Post < ActiveRecord::Base def author=(author) super unless self.published?