8

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?

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
Vishal
  • 7,113
  • 6
  • 31
  • 61
  • I checked your github profile, but I do not know which project are you talking about – Fabrizio Bertoglio Jan 06 '18 at 10:53
  • @FabrizioBertoglio this project is not in my github profile. Do you need project to solve my problem ? – Vishal Jan 07 '18 at 17:54
  • can you share the detailed logs. – Bijendra Jan 08 '18 at 11:27
  • I dont have any log, but when i user `current_admin_user` in audited.rb, it is storing user perfactly, when i use custom method `current_user_or_student` it is not storing. even exit is also not working for that method – Vishal Jan 08 '18 at 13:19
  • Can you provide the source of `current_admin_user`? Are you setting `Audited.current_user_method` in an initializer? Or are you attempting to change it dynamically at runtime? – Derek Prior Jan 08 '18 at 19:06
  • @DerekPrior I setting `Audited.current_user_method` in audited.rb initializer. It is working fine. because current_admin_user is method of devise. i want to customize method like when current admin user change details it should show current_admin_user, but when any student change details it should show student changed detail. student is just model it is not resource of devise and instructor is resource of devise. – Vishal Jan 09 '18 at 03:53

2 Answers2

2

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.

Vishal
  • 7,113
  • 6
  • 31
  • 61
0

You have defined current_user_or_student as such:

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

Assuming current_admin_user is nil, it will try to find InstructorStudent. You are using find_by_id which will return nil if the InstructorStudent with that id cannot be found. It's likely that is what is happening. It's not clear from this source what id is -- are you sure it's set to an id that can be found in instructor_students? I would encourage you to do the following to debug:

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

This will raise an error if the method's conditional falls through to the InstructorStudent.find branch and the id cannot be found. If my hypothesis is correct, this will prove that the id cannot be found, so your original code is returning nil (while this updated code now errors instead). The error message will also tell you what the value of id is which you can use for further debugging.

Alternatively, you can debug this without changing code by running a request that you expect to be audited but isn't and then inspecting the server logs. You will see the queries run there and may be able to debug that way as well.

Derek Prior
  • 3,497
  • 1
  • 25
  • 30
  • i think i already resolved this issue, i just need to add before_action :current_user_or_student ,in application_controller.rb everything else was working fine. and now it is also working fine – Vishal Jan 09 '18 at 16:10
  • It is not working in that way, i dont what is wrong with my application controller, even ` before_filter :audited_user` is also not working . and it is not calling before any action :( – Vishal Jan 09 '18 at 18:13