I have a scenario were I would like to inject the IP address and
UserAgent into an email when a database record is saved.
You need to trap the remote_ip and user_agent from the request method
in the Controller instance for that request. That is where this
information is received. Then you need to have this information
available in the Active Record instance where the save is happening.
One way to do this is to use the Thread class since both operations
are ocurring on the same thread. There is a Thread.current object that
you can stuff the info you need.
In your application.rb controller file:
class ApplicationController < ActionController::Base
Thread.current[‘remote_ip’] = request.remote_ip
Thread.current[‘user_agent’] = request.user_agent
Then somewhere your rails load path, either in lib or your models
directory, add an active record extension.
puts Thread.current[‘user_agent’] # do you really want to email
every request? you might want to do something else
Hope that helps.