# Weird assignment problem, very confused :(

**URL:** <https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213>\
**Category:** Rails\
**Created:** [August 29, 2011, 6:06pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213 "2011-08-29T18:06:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 29, 2011, 6:06pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/1 "2011-08-29T18:06:31Z")

</div>

Hey, I have the following line in my controller:

User.create(:email =\> “removed\_email\_address@domain.invalid”, :password =\>  
‘asldfkjadsfadsf’, :ip =\> request.remote\_ip)

my IP is 127.0.0.1 - now User.find\_by\_ip(‘127.0.0.1’) returns 0  
records although User.first contains ‘127.0.0.1’

if I change it to:

User.create(:email =\> “removed\_email\_address@domain.invalid”, :password =\>  
‘asldfkjadsfadsf’, :ip =\> ‘127.0.0.1’)  
the finder returns the record

The model is a devise model, ip is attr\_accessible  
Can anyone please help me getting out of this confusion? I don’t  
understand it

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 29, 2011, 7:36pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/2 "2011-08-29T19:36:07Z")

</div>

This is really weird, either a bug (it’s 3.1 rc5) or I don’t know:

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

DocType.create(:name =\> “127.0.0.1”)  
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  
=\> #\<DocType id: 3, name: “127.0.0.1”, description: nil\>

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 29, 2011, 8:04pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/3 "2011-08-29T20:04:30Z")

</div>

On Aug 29, 6:34pm, sol [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is really weird, either a bug (it’s 3.1 rc5) or I don’t know:
> 
> 1. 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

So what’s actually in the database in that case? Any unexpected  
characters (new lines etc.) ?

Fred

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 29, 2011, 8:06pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/4 "2011-08-29T20:06:13Z")

</div>

What does `User.first.ip.inspect` return in the two cases?

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 29, 2011, 11:10pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/5 "2011-08-29T23:10:25Z")

</div>

how can I get more details about the encoding/chars in the dbconsole?

sqlite\> select name from doc\_types;  
127.0.0.1  
127.0.0.1

On 29 Aug., 20:02, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 29, 2011, 11:14pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/6 "2011-08-29T23:14:45Z")

</div>

inspect returns:

ruby-1.9.2-p180 :009 \> DocType.all.map {|d| d.name.inspect}  
DocType Load (1.4ms) SELECT “doc\_types”.\* FROM “doc\_types”  
=\> ["“127.0.0.1"”, "“127.0.0.1"”]

and as above find only returns on of them (the one using  
request.remote\_ip

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 29, 2011, 11:40pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/7 "2011-08-29T23:40:38Z")

</div>

the output for the above lines is:

String  
127.0.0.1 (.ip)  
127.0.0.1 (.ip.to\_s)  
127.0.0.1 “#{d.ip}”  
“127.0.0.1” ip.inspect

no idea what this could be else… the size is 9 in all examples

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 29, 2011, 11:14pm UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/8 "2011-08-29T23:14:45Z")

</div>

Quoting sol [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> This is really weird, either a bug (it’s 3.1 rc5) or I don’t know:
> 
> 1. 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.

puts request.remote\_ip.class  
puts request.remote\_ip  
puts request.remote\_ip.to\_s  
puts “#{request.remote\_ip}”  
puts request.remote\_ip.inspect

DocType.create(:name =\> request.remote\_ip)  
DocType.last  
DocType.create(:name =\> request.remote\_ip.to\_s)  
DocType.last

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.

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 12:44am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/9 "2011-08-30T00:44:06Z")

</div>

Christoph B. wrote in post #1019055:

> Hey, I have the following line in my controller:
> 
> User.create(:email =\> “[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”, :password =\>  
> ‘asldfkjadsfadsf’, :ip =\> request.remote\_ip)
> 
> now User.find\_by\_ip(‘127.0.0.1’) returns 0  
> records
> 
> if I change it to:
> 
> User.create(:email =\> “[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”, :password =\>  
> ‘asldfkjadsfadsf’, :ip =\> ‘127.0.0.1’)  
> the finder returns the record

That seems like definitive proof that:

request.remote\_ip != ‘127.0.0.1’

In fact you could test that:

if request.remote\_ip == ‘127.0.0.1’  
puts ‘yes’  
else  
puts ‘no’  
end

> inspect returns:
> 
> ruby-1.9.2-p180 :009 \> DocType.all.map {|d| d.name.inspect}  
> DocType Load (1.4ms) SELECT “doc\_types”.\* FROM “doc\_types”  
> =\> [“"127.0.0.1"”, “"127.0.0.1"”]

The output shows that the string is actually “127.0.0.1”–not 127.0.0.1.  
For example:

data = [  
%q{“127.0.0.1”},  
%q{127.0.0.1}  
]

data.each do |str|  
p str  
end

–output:–  
“"127.0.0.1"”  
“127.0.0.1”

See the difference?

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 9:17am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/10 "2011-08-30T09:17:18Z")

</div>

I am sorry, Christoph, but from your post it is not clear, how you  
create a record with :ip =\> request.remote\_ip

You probably cannot do this in console (request.remote\_ip not defined),  
so you’ll have to test it all in controller.

Can you do it in 3 steps:

1. create two records in the database (with a string and with  
request.remote\_ip ),
2. see what is in the database (there is, for example, a firefox  
extension to browse an sqlite database),
3. verify in console that the records look identical, but find\_by\_ip  
returns only one?

This is what i would do.

Alexey.

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 9:04am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/11 "2011-08-30T09:04:55Z")

</div>

7stud – wrote in post #1019114:

> > inspect returns:
> > 
> > ruby-1.9.2-p180 :009 \> DocType.all.map {|d| d.name.inspect}  
> > DocType Load (1.4ms) SELECT “doc\_types”.\* FROM “doc\_types”  
> > =\> [“"127.0.0.1"”, “"127.0.0.1"”]
> 
> 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.

Alexey.

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 10:29am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/12 "2011-08-30T10:29:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 10:10am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/13 "2011-08-30T10:10:46Z")

</div>

I found out with sqlite’s ‘dump’ why it does not work, here is the  
full thing:

> <https://gist.github.com/solars/9fa01f6d150c3be6bd04>

it seems its in a different representation, now I am not sure if this  
is a rails bug or what to do with it

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 10:41am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/14 "2011-08-30T10:41:47Z")

</div>

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.

I use rails 3.1rc6 and ruby 1.9.2

Alexey.

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 11:06am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/15 "2011-08-30T11:06:01Z")

</div>

I also use sqlite in development, this is from my Gemfile:

group :development do  
gem ‘sqlite3’  
end

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 11:44am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/16 "2011-08-30T11:44:48Z")

</div>

On Aug 30, 9:07am, sol [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I found out with sqlite’s ‘dump’ why it does not work, here is the  
> full thing:
> 
> [gist:9fa01f6d150c3be6bd04 · GitHub](https://gist.github.com/9fa01f6d150c3be6bd04)
> 
> 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)

Fred

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 11:01am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/17 "2011-08-30T11:01:31Z")

</div>

Thanks a lot for the efforts!

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

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 11:57am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/18 "2011-08-30T11:57:10Z")

</div>

Thanks a lot Alexey, very helpful!

I’ve opened a bug but I’m not sure if it is rails or sqlite3 related:

> <https://github.com/rails/rails/issues/2743>
>
> Hey, I need some clarification because I really don't understand it
> 
> If I assign… request.remote\_ip to an objects attribute and persist it, its not found again because apparently its stored in hex representation in the database.
> 
> I explained the process here: https://gist.github.com/9fa01f6d150c3be6bd04
> 
> I'm currently using rails 3.1 rc8
> The actual method is calling .to\_s on the obtained ip
> 
> can anyone explain this to me please?

so I also opened it in sqlite3:

> <https://github.com/sparklemotion/sqlite3-ruby/issues/48>
>
> Hey,
> 
> I actually filed a rails bug about this, but I think it might rather be sq…lite3:
> 
> Here is the explanation: https://gist.github.com/9fa01f6d150c3be6bd04
> 
> I'm storing a String class value in the database, and somehow it is represented in hex - not sure if sqlite3 does this but I assume it might be okay.
> 
> However, a where query like select \\\* from doc\_types where name = '127.0.0.1' does not return the result then
> 
> Can anyone please enlighten me a bit? I'm very confused

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 11:49am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/19 "2011-08-30T11:49:42Z")

</div>

I confirm that there is a problem.  
This is with Rails 3.1.0rc8

$ rails new test\_ip  
$ cd test\_ip  
$ rails generate model MyModel ip:string description:string  
$ rake db:migrate  
$ rails generate controller Writer write\_records

app/controllers/writer\_controller.rb:

class WriterController \< ApplicationController  
def write\_records  
MyModel.create!(:ip =\> request.remote\_ip, :description =\> ‘request’)  
MyModel.create!(:ip =\> ‘127.0.0.1’, :description =\> ‘string’)  
end  
end

$ rails s

go to  
[http://localhost:3000/writer/write\_records](http://localhost:3000/writer/write_records)

$ rails c

> MyModel.all  
> MyModel Load (0.2ms) SELECT “my\_models”.\* FROM “my\_models”  
> =\> [#\<MyModel id: 1, ip: “127.0.0.1”, description: “request”,  
> created\_at: “2011-08-30 09:42:09”, updated\_at: “2011-08-30 09:42:09”\>,  
> #\<MyModel id: 2, ip: “127.0.0.1”, description: “string”, created\_at:  
> “2011-08-30 09:42:09”, updated\_at: “2011-08-30 09:42:09”\>]

> MyModel.where(:ip =\> ‘127.0.0.1’).all  
> MyModel Load (0.2ms) SELECT “my\_models”.\* FROM “my\_models” WHERE  
> “my\_models”.“ip” = ‘127.0.0.1’  
> =\> [#\<MyModel id: 2, ip: “127.0.0.1”, description: “string”,  
> created\_at: “2011-08-30 09:42:09”, updated\_at: “2011-08-30 09:42:09”\>]

> MyModel.first.ip == ‘127.0.0.1’  
> MyModel Load (0.2ms) SELECT “my\_models”.\* FROM “my\_models” LIMIT 1  
> =\> true

In the database for the first ip i have: X’3132372E302E302E31’

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [August 30, 2011, 11:58am UTC](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213/20 "2011-08-30T11:58:03Z")

</div>

Alexey M. wrote in post #1019158:

> 7stud – wrote in post #1019114:
> 
> > > inspect returns:
> > > 
> > > ruby-1.9.2-p180 :009 \> DocType.all.map {|d| d.name.inspect}  
> > > DocType Load (1.4ms) SELECT “doc\_types”.\* FROM “doc\_types”  
> > > =\> [“"127.0.0.1"”, “"127.0.0.1"”]
> > 
> > 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.

[Next page](https://www.ruby-forum.com/t/weird-assignment-problem-very-confused/211213.md?page=2)
