我有代表的評論的(很簡單)模型:Rails:與class_name的關係無法正確保存?
class Comment < ActiveRecord::Base
STATES = [:processing, :accepted, :declined]
belongs_to :note
belongs_to :author, :class_name => 'User'
validates_inclusion_of :state, :in => STATES
validates_presence_of :author
default_scope :order => 'created_at DESC'
def initialize(attributes={})
super(attributes)
self.state ||= 'processing'
end
end
然而,每次我保存評論(其字段中設置正確),筆者關係始終無法保存(當然,實際上的評論保存成功,它只是遺漏了作者......)。由於在作者字段上的驗證(Comment.first.author
是nil
),因此這可以返回Comment.first.valid?
返回false
。
我的懷疑是我以錯誤的方式處理狀態字段的默認值?如果是這樣,我應該如何設置默認值呢?提前
您可能想看看https://github.com/pluginaweek/state_machine – Luke
這是否僅在創建新評論或更新現有評論時發生? – Luke
當我更新現有評論時,也會發生這種情況 – DeX3