开发者

Can I automatically save the current user details using ActiveRecord callbacks?

开发者 https://www.devze.com 2023-02-15 03:18 出处:网络
I\'d like to use ActiveRecord\'s before_save callback to help me track which users edit which records. For example

I'd like to use ActiveRecord's before_save callback to help me track which users edit which records. For example

class Foo < ActiveRecord::Base
  before_save
   self.edited_by = curent_user.username
  end
  ....
end

The problem I'm running into h开发者_高级运维ere is that Foo has no idea what current_user is because that's coming from the controller helper.

I could do this all in the controller, I realize. But it'd be nice to just drop this into the before_save callback if I could.


Easiest way to do this is to make your model richer:

class Article
  def edit!(editor)
    self.edited_by = editor.name
    self.save!
  end
end

No need to use callbacks.

0

精彩评论

暂无评论...
验证码 换一张
取 消