鉴于以下模型:
Room (id, title)
RoomMembers (id, room_id)
RoomFeed, also an observer
当房间标题更新时,我想创建一个 RoomFeed 项目,显示进行更新的用户是谁。
@room.update_attributes(:title => "This is my new title")
问题出在我的 RoomFeed 观察者身上:
def after_update(record)
# record is the Room object
end
我无法获得刚刚进行更新的人的 user.id。我该怎么做?有没有更好的方法来进行更新,以便我获得 current_user?
最佳答案
我认为您正在寻找的是观察者内部的 room.updated_by。如果您不想保留updated_by,只需将其声明为attr_accessor。在推送更新之前,请确保将 current_user 分配给 updated_by,可能来自您的 Controller 。
关于ruby-on-rails - 如何在模型观察者中获取 current_user?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7181036/