我有限定enum像這樣一個模型:ActiveRecord的枚舉和髒屬性
enum status: [ :init, :requested, :packed, :paid, :shipped ]
我也有其使用髒屬性下面的方法。
def shipment_requested
status_changed?(from: :init, to: :requested)
end
def shipment_packed
status_changed?(from: :requested, to: :packed)
end
def shipment_paid
status_changed?(from: :packed, to: :paid)
end
def shipment_shipped
status_changed?(from: :paid, to: :shipped)
end
有很多模型回調依賴於此,如更新時間戳和發送電子郵件。
但不幸的是沒有這個作品。
sh = Shipment.find(1)
sh.init?
=> true
sh.requested!
sh.requested?
=> true
sh.shipment_requested
=> false
上述代碼有什麼問題?這是一個錯誤?
我可以在控制器手動設置所有的值,但這種違背了使用其自帶的相當不錯的功能,按照上面的文檔枚舉的目的,類似定義範圍和方法來檢查status
什麼是'status_changed?'? – Pavan
[AASM](https://github.com/aasm/aasm)正是爲這種情況而設計的。 – 31piy
@Pavan [rails dirty屬性](http://api.rubyonrails.org/classes/ActiveModel/Dirty.html) – static