Hi, How can i access to request.remote_ip in a model?
request.env["REMOTE_ADDR"]
One way would be to set a Thread local variable in a before_filter. See: Zorched / One-Line Fix | Making Session Data Available to Models in Ruby on Rails
Cheers, Rob
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.
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
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.
i want ip address for user model on a "before_create" method, and store ip address on each connection.
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