set user IP on create

i tried to make a callback to assign the user IP address on create

before_create
self.ip = request.remote_ip
end

but I get

NameError (undefined local variable or method `request' for
#<User:0x5b05078>):

I think (though not certain) that request is only available in the
controller and since the callback is in the model then request is not
available.

Colin

Yep. Models don’t (and probably shouldn’t) know about the request at all.

so how should I add the IP? assign it on the controller or maybe a
hidden text field?

so how should I add the IP? assign it on the controller or maybe a
hidden text field?

http://lmgtfy.com/?q=rails+ip+address

You’ll have access to the request in the controller.

Assigning it in the controller is probably the best bet:

instance = Model.new(params[:model])
instance.ip_address = request.ip_address

Tim Shaffer wrote in post #1012223:

Assigning it in the controller is probably the best bet:

instance = Model.new(params[:model])
instance.ip_address = request.ip_address

I will do it then, thanks