Hi, How can i access to request.remote_ip in a model?
on 2007-07-09 11:38
on 2007-07-09 12:55
on 2007-07-09 13:05
One way would be to set a Thread local variable in a before_filter. See: http://www.zorched.net/2007/05/29/making-session-d... Cheers, Rob
on 2007-07-09 14:32
duh, i misread the OP, didn't see the "in a model" part. i would recommend you pass it as a parameter to a method.
on 2007-07-10 00:31
so that's able to call it from parent object, something like it (i'm not sure about the parent class) : CGI::Request.remote_ip
on 2007-07-10 01:27
i didn't really understand your reply. to be clear, what you want to do is pass the remote ip in as a parameter to one of the models methods. I'm not sure what it is you are trying to accomplish, so without further info, there's not much more assistance i can provide.
on 2007-07-10 10:03
i want ip address for user model on a "before_create" method, and store ip address on each connection.
on 2007-07-10 16:46
what i might do in this case is instantiate a new user object and initialize it with the ip you want to store. keep this instance in the session (below is just an idea, not tested) ApplicationController < ActionController::Base before_filter :init_user private def init_user session[:user] ||= User.new(:ip => request.env["REMOTE_IP"]) end end then when you are ready, just save the user object from the session session[:user].save and it will already have the ip address. no need for anything fancy again, not sure if that would work, but that is route i would take for something like this. hope that helps. Chris