Search code examples
rubyruby-on-rails-3observer-pattern

Rails 3 observers and current_user


There is few similar questions on Stackoverflow about getting of current_user form Rails 3 observers, but basic ideas the same:

1) passing of current_user via

attr_accessor :current_user

in the model we need to observe.

But this unacceptable for me, as, i need to observer 10 models (create, update, delete actions), and i don't want to pass this on each action, for each controller.

2) Storing current user in Thread. Completely unacceptable.

So what is the best practice? And, let's say, i want to get not only current_user in my observer, but also, IP address?

Any ideas?


Solution

  • After investigating for more, i found it's totally useless to use Rails native observers in a such a tasks. Because, if you need to pass something from controllers, in my case it was current_user, this totally ruins observer's beauty.

    So my answer to my own question is: still stuck with something like acts_as_audited