# IP Address

**URL:** <https://www.ruby-forum.com/t/ip-address/94894>\
**Category:** Rails\
**Created:** [January 10, 2006, 5:42am UTC](https://www.ruby-forum.com/t/ip-address/94894 "2006-01-10T05:42:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [January 10, 2006, 5:42am UTC](https://www.ruby-forum.com/t/ip-address/94894/1 "2006-01-10T05:42:27Z")

</div>

There’s got to be a simple answer: how do you get the IP address of a  
visitor in Ruby? Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [January 10, 2006, 5:54am UTC](https://www.ruby-forum.com/t/ip-address/94894/2 "2006-01-10T05:54:48Z")

</div>

On 1/10/06, Ben CH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There’s got to be a simple answer: how do you get the IP address of a  
> visitor in Ruby? Any help would be appreciated!

[http://api.rubyonrails.com/classes/ActionController/AbstractRequest.html#M000149](http://api.rubyonrails.com/classes/ActionController/AbstractRequest.html#M000149)

So, for example, request.host or request.remote\_ip in your controller  
should work.

Ben

---

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [January 10, 2006, 6:58am UTC](https://www.ruby-forum.com/t/ip-address/94894/3 "2006-01-10T06:58:54Z")

</div>

So, it looks like model classes that inherit from ActiveRecord::Base  
have:

–automagic getters and setters behind the scenes  
–that are class methods

(1) But it looks like there are some constraints I wasn’t aware of.  
For example, it doesn’t seem like you can override any of those  
getters and provide a custom one by doing something like:

class Dog \< ActiveRecord::Base  
def bark  
return (bark.to\_s + " and bite")  
end  
end

… and then call it with: a\_dog.bark Why not? Maybe because  
you can only have class methods that return a Model object, and this  
does neither???

(2) Also, it doesn’t seem like I can add an instance variable to the  
a Model class:

class Dog \< ActiveRecord::Base  
def initialize  
@favorite\_treat = “chew toy” #where ‘favorite\_treat’ is not a  
column  
end  
end

Why not? Because the model should be kept true to the db??

(3) Lastly, I’m pretty sure I should be able to return a new model  
object with pre-set attributes, but haven’t been able to.

class Dog \< ActiveRecord::Base  
def self.get\_preset\_object  
a\_dog = self.new  
a\_dog.bark = “bowwow”  
a\_dog.fur = “brown”  
a\_dog  
end  
end

Any explanations appreciated. Thanks,  
Russ

---

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [January 12, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/ip-address/94894/4 "2006-01-12T18:37:58Z")

</div>

I sorted this out. If you’re interested…

> > end
> 
> That should work: Check the ActiveRecord documentation:
> 
> [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Base.html)
> 
> and see the section “Overriding default accessors”

You were right. I just needed to use  
write\_attribute(:some\_attribute) and read\_attribute(some\_attribute)  
here.

> and call the original initialize method before your initializations:  
> … same thing here

> Warning: Untested!
> 
> class Foo \< ActiveRecord::Base
> 
> ```
> alias :orig_initialize, :initialize
> def initialize
> orig_initialize
> @my_stuff = "bla"
> end
> 
> ```
> 
> end

…haven’t tried this yet

> > ```
> > a_dog
> > 
> > ```
> > 
> > end  
> > end
> 
> Use Model.create  
> [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000718)
> 
> a\_dog = Dog.create( :bark =\> “bowwow”, :fur =\> "brown )

Create would certainly work. And you can use the write\_attribute  
again here I think.

Thanks,  
russ

---

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [January 15, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/ip-address/94894/5 "2006-01-15T20:53:33Z")

</div>

> … same thing here

Actually: Don’t override the initialize method, but instead use a  
callback:

class Dog \< ActiveRecord::Base

after\_initialize :my\_init

def my\_init  
@favorite\_treat = “chew toy”  
end  
end

cu jc

---

<div class="post-metadata">

**Author:** ![bch36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/858c86/32.png) [@bch36](https://www.ruby-forum.com/u/bch36)\
**Post date:** [January 10, 2006, 9:53am UTC](https://www.ruby-forum.com/t/ip-address/94894/6 "2006-01-10T09:53:58Z")

</div>

> (1) But it looks like there are some constraints I wasn’t aware of.  
> For example, it doesn’t seem like you can override any of those  
> getters and provide a custom one by doing something like:
> 
> class Dog \< ActiveRecord::Base  
> def bark  
> return (bark.to\_s + " and bite")  
> end  
> end

That should work: Check the ActiveRecord documentation:

[http://api.rubyonrails.com/classes/ActiveRecord/Base.html](http://api.rubyonrails.com/classes/ActiveRecord/Base.html)

and see the section “Overriding default accessors”

> class Dog \< ActiveRecord::Base  
> def initialize  
> @favorite\_treat = “chew toy” #where ‘favorite\_treat’ is  
> not a column  
> end  
> end

There’s no problem adding instance variables. There might be a  
problem with you overriding the initialize method. Try to alias it  
and call the original initialize method before your initializations:

Warning: Untested!

class Foo \< ActiveRecord::Base

```
 alias :orig_initialize, :initialize
 def initialize
     orig_initialize
     @my_stuff = "bla"
 end

```

end

> end  
> end

Use Model.create [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/)  
Base.html#M000718

a\_dog = Dog.create( :bark =\> “bowwow”, :fur =\> "brown )

cu jc
