5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

paper_trailsでデータ変更時のコメントを保存する

Posted at

参考

手順

versionテーブルにcommentフィールドを追加する

migrationファイル作成

$ ./bin/rails g migration add_comment_to_versions comment:text
# 20150811184014_add_comment_to_versions.rb:

class AddCommentToVersions < ActiveRecord::Migration
  def change
    add_column :versions, :comment, :text
  end
end

migration実行

$ ./bin/rake db:migrate

モデルの修正

class Post < ActiveRecord::Base
  has_paper_trail :meta => { :comment => :comment }

  attr_accessor :comment
end

上記の設定後、Postモデル変更時(saveupdate_attributes)のパラメータにcommentを渡すと、Versionテーブルにコメントを保存することができます。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?