LoginSignup
18
15

More than 5 years have passed since last update.

railsでデータを登録、更新したユーザーの情報がほしい

Last updated at Posted at 2014-09-24

やりたいこと

データを登録した人、更新した人の情報がほしい

どうやるか

record_with_operatorを使う

前提

Gemfile
gem 'devise'
% bundle
% rails g devise:install
% rails g devise User
% rake db:migrate

導入

Gemfile
gem 'record_with_operator'
% bundle
% rails g migration add_operator_to_model created_by:integer updated_by:integer
% rake db:migrate
application_controller.rb
  before_action do
    RecordWithOperator.operator = current_user
  end
model
  records_with_operator_on :create, :update

これで登録、更新時のユーザーIDが勝手に登録される

使い方

  = " #{ User.find(post.created_by).name }#{ l post.created_at }に投稿"

参考

record_with_operator

18
15
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
18
15