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! -- Posted via http://www.ruby-forum.com/.
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/ActionController/AbstractRequest.html#M000149 So, for example, request.host or request.remote_ip in your controller should work. Ben
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
> (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 > endThat 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 > endThere''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> > (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 > endUse Model.create http://api.rubyonrails.com/classes/ActiveRecord/ Base.html#M000718 a_dog = Dog.create( :bark => "bowwow", :fur => "brown ) cu jc -- InVisible GmbH, Langgr?tstrasse 172, 8047 Z?rich Phone: +41 44 401 09 30 http://www.invisible.ch http://not.invisible.ch -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2361 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060110/fe93a3c2/smime.bin
I sorted this out. If you''re interested...> > >>(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"You were right. I just needed to use write_attribute(:some_attribute) and read_attribute(some_attribute) here.> >> >>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:... 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> >> >>(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 > >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
>>> 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: > > > ... 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 -- InVisible GmbH, Langgr?tstrasse 172, 8047 Z?rich Phone: +41 44 401 09 30 http://www.invisible.ch http://not.invisible.ch -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2361 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060115/32799cfb/smime.bin