You are having problems with what you want because Rails does not allow you to have access to session information in your models. This is a classic separation of issues with MVC. Models are designed to work regardless of your other levels, and you will be grateful to them when you start to do something using Rake or other system tasks where you will not have a session.
cattr_accessor :current_ip
is a terrible approach. This is a hack, and it should be clear why. Yes, it may work, but this is the wrong approach to this problem.
Since you are tracking the βwhoβ has done the βwhatβ by their IP, the logical place for this happens in the controller layer. There are several approaches, including using CacheSweepers as auditors, as described in the Rails Recipes book. CacheSweepers can monitor models, but also have access to all controller information. Using the ditry attributes in the rail model, you can see exactly what has changed.
@user = User.find_by_login "bphogan" @user.login = "Brian" @user.save @user.changed => ["login"] @user.changes => {"login"=>["bphogan", "brian"]} @user.login_was => "bphogan"
Combine this with the session information you have, and you have a pretty amazing audience.
Does it help?
source share