如何获得模型中观测的CURRENT_USER? [英] How to get the current_user in a model observer?

查看:135
本文介绍了如何获得模型中观测的CURRENT_USER?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下型号:

Room (id, title)
RoomMembers (id, room_id)
RoomFeed, also an observer

在一个房间标题更新,我想创建一个RoomFeed项目,显示出用户是谁谁做的更新。

When a Room title is updated, I want to create a RoomFeed item, showing who the user is who made the update.

@room.update_attributes(:title => "This is my new title")

但问题是在我的观察员RoomFeed:

Problem is in my observer for RoomFeed:

def after_update(record)
   # record is the Room object
end

本是没有办法,我让谁刚才提出的更新的人的user.id。我该如何去这样做?有没有更好的方式来做到更新,所以我得到了CURRENT_USER?

The is no way for me to get the user.id of the person who just made the update. How do I go about doing that? is there a better way to do the update so I get the current_user?

推荐答案

我觉得你在找什么是room.updated_by你的观察中。如果你不想坚持了updated_by,刚刚宣布它作为一个attr_accessor。在你推送更新,请确保您指定CURRENT_USER到updated_by,可能与您的控制器。

I think what you are looking for is, room.updated_by inside your observer. If you don't want to persist the updated_by, just declare it as an attr_accessor. Before you push the update, make sure you assign the current_user to updated_by, may be from you controller.

这篇关于如何获得模型中观测的CURRENT_USER?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆