【发布时间】:2020-01-06 05:34:35
【问题描述】:
我有这个多态模型。
class Note < ApplicationRecord
belongs_to :notable, polymorphic: true, optional: false
state_machine :is_status, initial: :pending do
transition pending: :approved, on: %i[approve]
transition pending: :cancelled, on: %i[cancel]
end
end
和其他两个模型
class Invoice < ApplicationRecord
has_many :notes, as: :notable, dependent: :destroy
end
class Person < ApplicationRecord
has_many :notes, as: :notable, dependent: :destroy
end
如您所见,我在两个模型中附加了个人或发票的注释。并且还使用状态机。场景是我只想在invoice 中使用状态机?这可能吗。
所以。如果我的 notable_type 是“发票”。我的状态是“待定”,否则我的状态是 Person:nil
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5 state-machine