Search code examples
ruby-on-railsacts-as-audited

Audited Gem current_user_method not working


I am using the Audited gem in my application for tracking user log. Everything is working fine except for current user tracking.

In my case, I have 2 models: Instructor and Student. Instructor will be the current_admin_user, and I need to find the student manually.

To overcome this issue, I tried to override current_user_method and create an audited.rb file in the initializers with below content:

Audited.current_user_method = :current_admin_user

This is working fine, but when I use any other method like current_user_or_student ...

Audited.current_user_method = :current_user_or_student

in application_controller.rb ...

def current_user_or_student
  current_admin_user || InstructorStudent.find_by_id(id)
end

it is not going into this method, even current_admin_user is also not storing in audits.

Why isn't my current_user_or_student method being called when overriding it in application_controller.rb?


Solution

  • Finally I resolved my issue, I am using STI (single table inheritance) . my other controllers of instructor was not inherite from application controller. so my current_user_or_student was not called for instructor login .

    To overcome this issue, i create one controller concern called audited_user.rb

    and write following method in concern

    def current_user_or_student
      current_admin_user || InstructorStudent.find_by_id(id)
    end
    

    and included this concern in both my instructor base controller and application controller. now everything is working fine and my audited user is also save correctly.

    I hope this will save any others day.