Forum: Ruby on Rails IP Address

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D29980bb76cbac0f844c84dfcc52dadf?d=identicon&s=25 Ben Ch (bch36)
on 2006-01-10 05:42
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!
C319ec7828874b714f89fdb2200de905?d=identicon&s=25 Ben Myles (Guest)
on 2006-01-10 05:54
(Received via mailing list)
On 1/10/06, Ben CH <eviloverlord@gmail.com> 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/ActionControlle...

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

Ben
78316e522d62b1656f6e2e0cea8f90e6?d=identicon&s=25 Russ McBride (Guest)
on 2006-01-10 06:58
(Received via mailing list)
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
5085ba37ba624a788e04a842cd09bd6e?d=identicon&s=25 Jens-Christian Fischer (Guest)
on 2006-01-10 09:53
(Received via mailing list)
> (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
78316e522d62b1656f6e2e0cea8f90e6?d=identicon&s=25 Russ McBride (Guest)
on 2006-01-12 18:37
(Received via mailing list)
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/Ba...
>
>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
5085ba37ba624a788e04a842cd09bd6e?d=identicon&s=25 Jens-Christian Fischer (Guest)
on 2006-01-15 20:53
(Received via mailing list)
>
>
> ... 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
This topic is locked and can not be replied to.