Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current_user in a model observer?

Given the following models:

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

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")

Problem is in my observer for RoomFeed:

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

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?

like image 396
AnApprentice Avatar asked Aug 24 '11 19:08

AnApprentice


1 Answers

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.

like image 62
Sohan Avatar answered Sep 24 '22 22:09

Sohan