Rails : Accessing session data in models

MVC architecture is good for keeping your design neat but once in a while you hit a road block and this is one such situtation.  If you want to log actions/events in your code and track changes to models, you would want to do that in the activerecord callback hooks like after_save and before_destroy. However these methods have only access to the record that is  affected and not to the session data. Therefore, you cannot log ‘who’ made the change.

After some googling around and wasting an hour or so, I found out that the trick is to use cache_sweeper class, which keeps a track of the objects that you ask it to for regenerating caches. You can however use it to keep a track of your changes and log it. Cache Sweeper objects also have access to the session data so you can know ‘who’ made changes to your model.

The process is described further in Rails Recipes book and is quite interesting … go read on :)

Popularity: 17% [?]

find this blog feedworthy. Click on the number to subscribe!

Related Posts

ruby’s respond_to? for checking if a method exists

Move to Rails 2.0.2

Ruby issues and backgroundrb

Comments

No comments yet.

Leave a comment

(required)

(required)