IP Address


#1

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!


#2

On 1/10/06, Ben CH 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

So, for example, request.host or request.remote_ip in your controller
should work.

Ben


#3

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


#4

I sorted this out. If you’re interested…

end

That should work: Check the ActiveRecord documentation:

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
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


#5

… 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


#6

(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

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 http://api.rubyonrails.com/classes/ActiveRecord/
Base.html#M000718

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

cu jc