This is really weird, either a bug (it’s 3.1 rc5) or I don’t know:
DocType.create(:name => request.remote_ip)
DocType.find_by_name(‘127.0.0.1’)
DocType Load (1.4ms) SELECT “doc_types”.* FROM “doc_types” WHERE
“doc_types”.“name” = ‘127.0.0.1’ LIMIT 1
=> nil
Dynamic typing is wonderful, until it isn’t. My hypothesis is that
remote_ip
is not a Fixnum or Bignum but a custom class that overrides to_s. To
test the
hypothesis, I’d run the following in the console of writing to the log.
Somewhere in this, I expect the truth will be revealed.
A IPv4 address is a 32 bit, unsigned number. It is usually
represented/rendered in dotted quad notation, but 0x7F000001 or
2130706433 are
equally valid representations of the local or loopback interface’s IPv4
address.
The output shows that the string is actually “127.0.0.1”–not 127.0.0.1.
I disagree, this output shows only that the inspect of the string is
(unquoted) “127.0.0.1”, but the string itself is (unquoted) 127.0.0.1.
So this does not clarify the issue.
This is strange, given that request.remote_ip.class returns String.
What if you use request.remote_ip.to_s instead of request.remote_ip everywhere?
If this does not fix it, than it looks like a bug to me.
I also have an application where i store IP addresses, but i didn’t try
yet to find by ip, and the IP returned by request.remote_ip looked
like a string to me.
I would be interested to understand what’s the difference.
Christophe,
I have just tested my application: i use request.remote_ip, and find_by_ip('127.0.0.1') works for me.
If you describe a minimal example how to reproduce your problem, i can
try to test it.
it seems its in a different representation, now I am not sure if this
is a rails bug or what to do with it
That byte sequence is in fact exactly the same as 127.0.0.1 (if you
encoding is ascii or anything that agrees with ascii for the lower 7
bits). For me this points at an encoding problem, but I’m not sure why
this would happen. You might try sticking some breakpoints in the
active record code to see what the difference is (or check if the ruby
encoding of the two strings you have differ)
The example should be in the gist I posted above, thats exactly what I
am doing
However, which database are you using? It is sqlite for me, and I
think it might be the sqlite driver
The output shows that the string is actually “127.0.0.1”–not 127.0.0.1.
I disagree,
Okay, I agree with your disagree. I have no idea what this means,
though:
this output shows only that the inspect of the string is
(unquoted) “127.0.0.1”, but the string itself is (unquoted) 127.0.0.1.
So this does not clarify the issue.
Alexey.
strings = [
“abc”,
%q(“abc”)
]
results = strings.map do |str|
str.inspect
end
p results
results.each do |str|
puts str
end
–output:–
["“abc”", “”\“abc\”""]
“abc”
““abc””
The second element in the array is hard to interpret: if you strip away
what inspect added to the first string, you get:
\“abc\”
I guess \ is a literal slash and ’ is an escaped quote.