如何在模型观察器中获取current_user?

AnA*_*ice 5 activerecord ruby-on-rails observer-pattern ruby-on-rails-3

鉴于以下型号:

Room (id, title)
RoomMembers (id, room_id)
RoomFeed, also an observer
Run Code Online (Sandbox Code Playgroud)

当房间标题更新时,我想创建一个RoomFeed项目,显示用户是谁进行了更新.

@room.update_attributes(:title => "This is my new title")
Run Code Online (Sandbox Code Playgroud)

我的观察者在RoomFeed中遇到了问题:

def after_update(record)
   # record is the Room object
end
Run Code Online (Sandbox Code Playgroud)

我无法获得刚刚进行更新的人的user.id. 我该怎么做呢?有没有更好的方法来进行更新,所以我得到current_user?

Soh*_*han 5

我想你要找的是,你的观察者里面是room.updated_by.如果您不想持久化updated_by,只需将其声明为attr_accessor即可.在推送更新之前,请确保将current_user分配给updated_by,可能来自您的控制器.