改变STI类型会让人感到困惑

时间:2015-07-31 04:25:29

标签: ruby ruby-on-rails-4 rails-activerecord

class SubA < Base
end

class SubB < Base
  belongs_to :foo
  validates :foo, presence: true
end

copy = sub_a.dup.becomes SubB
copy.type = SubB.sti_name
copy.save!

保存后,我得到:

#<NoMethodError: undefined method `foo' for #<SubA:0x007faa7b64abb8>>

如果我删除验证,则错误消失。为什么ActiveRecord会调用SubB的验证,但后来认为它仍然是SubA?可能有错误吗?

0 个答案:

没有答案