acts_as_versioned 是個好套件。只要一點設定他就可以幫你紀錄model的版本變動。可是他預設沒有支援has_*/belongs_to的資料庫關聯,所以讓我們來看看要如何打開這個功能。
假設你要追蹤的是Post這個model,那你的model檔大概長這樣:
複製內容到剪貼板
代碼:
class Post < ActiveRecord::Base
acts_as_versioned
belongs_to :user
end但這個時候 version.user 是會掛掉的(至少這版套件如此)
(1) 套件版本較新者可以試試這個:
複製內容到剪貼板
代碼:
class Post < ActiveRecord::Base
acts_as_versioned do
belongs_to :user
end
end(2) 舊版者,則試這個:
複製內容到剪貼板
代碼:
class Post < ActiveRecord::Base
acts_as_versioned
belongs_to :user
end
Post.versioned_class.class_eval do
belongs_to :user
end我用第2個方案OK,但是第1個方案則不行... (ruby 1.8.6, rails 2.3.2, acts_as_versioned 0.3.1)
參考
這篇
也有發佈在
我的筆記blog