How to get the current_user in a model observer?

前端 未结 5 2011
有刺的猬
有刺的猬 2021-01-13 05:35

Given the following models:

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

When a Room title is updated, I want to c

5条回答
  •  清酒与你
    2021-01-13 05:53

    This is a typical "separation of concern" issue.

    The current_user lives in the controller and the Room model should know nothing about it. Maybe a RoomManager model could take care of who's changing the name on the doors...

    Meanwhile a quick & dirty solution would be to throw a (non persistant) attribute at Room.rb to handle the current_user....

    # room.rb
    class Room
      attr_accessor :room_tagger_id
    end
    

    and pass your current_user in the params when updating @room.

    That way you've got the culprit! :

    def after_update(record)
       # record is the Room object
       current_user = record.room_tagger_id
    end
    

提交回复
热议问题