标签: ruby-on-rails ruby-on-rails-4
我有以下STI模型:
class Question < ActiveRecord::Base class Unapproved < Question class Approved < Question
如果我的问题类型为Unapproved,我该如何将其转换为Approved类型?
Unapproved
Approved
答案 0 :(得分:4)
请参阅becomes和becomes!方法,例如:
becomes
becomes!
Unapproved.first.becomes!(Approved)
答案 1 :(得分:1)
我不认为Rails提供了一种自动切换的方法,但你应该可以这样做:
Approved.first.update(type: 'Unapproved')
OR
question = Approved.find(x) question.type = 'Unapproved' question.save
您也可以自己在Question课程中定义自动切换器
Question