I have an existing app that has many, many models. I’d like to log the IP address of the user that created them with the primary purpose being a way to help weed out spammers or other abusive users (if I don’t know what IP address(es) they are using, I can’t block it). I would need to keep track of these over time as users may access from home, the office, their phone, etc. and I’d want to see usage patterns. It also might be kind of fun to map out where users are visiting from or something, but any side-effects are purely thought-stuff at this time.
We use the cookie-based method of storing user sessions.
I can think of two ways of doing this (I’ll create them as replies so people can vote):
- Add IP address attribute to every model, and pass that in
- Some sort of Logger model that is called with an observer or after_save callback
Thoughts? Are there better ways? Plugins that do this? Thanks!!
I would use a polymorphic association for this, and then you can apply it to all your models that need to be tracked. That keeps the messyness of adding an IP address column to every model to a minimum and will dramatically cut down on the amount of duplication across your codebase. Then going the observer route is probably the easiest way to hook up to your models.