Model.find for attributes with long integers fails
chriso0710 opened this issue · 7 comments
Hi all,
I have the following model definition
class Message < Ohm::Model
attribute :mid
index :mid
...
I understand that ohm translates all attributes to strings. But when I try to find a message with a very large integer for mid, the find fails.
Small integers and strings work
2.2.1 :069 > m = Message.new(:mid => 4711)
#<Message:0x007fbc24ab72b8 @attributes={:mid=>4711, :score=>0}, @_memo={}>
2.2.1 :070 > m.save
#<Message:0x007fbc24ab72b8 @attributes={:mid=>4711, :score=>0}, @_memo={}, @id="1120356">
2.2.1 :072 > Message.find(:mid => "4711").to_a
[
[0] #<Message:0x007fbc259394d0 @attributes={:type=>"0", :mid=>"4711", :username=>"-", :received_at=>"0", :score=>"0"}, @_memo={}, @id="1120356">
]
2.2.1 :076 > m = Message.new(:mid => 1234567890)
#<Message:0x007fbc29821df0 @attributes={:mid=>1234567890, :score=>0}, @_memo={}>
2.2.1 :077 > m.save
#<Message:0x007fbc29821df0 @attributes={:mid=>1234567890, :score=>0}, @_memo={}, @id="1120360">
2.2.1 :078 > Message.find(:mid => "1234567890").to_a
[
[0] #<Message:0x007fbc26632920 @attributes={:type=>"0", :mid=>"1234567890", :username=>"-", :received_at=>"0", :score=>"0"}, @_memo={}, @id="1120360">
]
2.2.1 :081 > m = Message.new(:mid => "123456789012345678")
#<Message:0x007fbc25d35638 @attributes={:mid=>"123456789012345678", :score=>0}, @_memo={}>
2.2.1 :082 > m.save
#<Message:0x007fbc25d35638 @attributes={:mid=>"123456789012345678", :score=>0}, @_memo={}, @id="1120369">
2.2.1 :083 > Message.find(:mid => "123456789012345678").to_a
[
[0] #<Message:0x007fbc25da4c40 @attributes={:type=>"0", :mid=>"123456789012345678", :username=>"-", :received_at=>"0", :score=>"0"}, @_memo={}, @id="1120369">
]
This does not
2.2.1 :073 > m = Message.new(:mid => 123456789012345678)
#<Message:0x007fbc2a80a5c8 @attributes={:mid=>123456789012345678, :score=>0}, @_memo={}>
2.2.1 :074 > m.save
#<Message:0x007fbc2a80a5c8 @attributes={:mid=>123456789012345678, :score=>0}, @_memo={}, @id="1120359">
2.2.1 :075 > Message.find(:mid => "123456789012345678").to_a
[]
What am I doing wrong?
Or is that a bug? Do I explicitly have to cast all fields to string before saving the model?
Thanks and best regards
Christian
@chriso0710 Hey! It seems to be working for me. Can you tell me which version of Ohm you are using? If you are using the latest (3.1.1), can you tell me if there are other gems you are loading that may interfere?
Good point. I am still using version 2.3 for all environments.
2.3 is running great here and until now I had not seen any reason to update. I am somewhat hesitant (you might say conservative :-) to make the switch to 3.x and I did not have the time to test the update and migrate the data.
Was this a known bug in 2.3? Might be a reason for updating... although it could easily be worked around by casting the integer to string before saving.
Thanks for your continuous work on ohm!
It could be a serialization problem from Msgpack, which was in use in version 2.3. I can help you with the migration to 3.1.1 if you want. The first step would be to just install it in a branch of your project and check what errors you get.
I read the release notes of 3.0.0, which dropped msgpack and changes the internal JSON enconding. Sounds serious, but I still might give it a try.
Are there any other important changes in 3.x I should be aware of?
What kind of trouble with migrating existing model data from 2.3 should I expect?
I searched the issues for any update experience or update trouble, but could not find any, which might indicate, that the update process is smooth and flawless :-)
BTW: I am using ohm in a concurrent microservice architecture, with multiple workers running sinatra, event machine and suckerpunch.
Thanks again.
Just a quick addition:
I am also using Ohm::Callbacks from https://github.com/sinefunc/ohm-contrib (last commit in 2010!)
That repo was moved to https://github.com/cyx/ohm-contrib, and I have plans of moving some functionality to Ohm itself. I think that particular feature, the callbacks, is a great candidate to become part of Ohm.
I will close this issue because it's solved in the latest version, but keep commenting here if you need help with the migration to 3.1.1.