Вопрос

i have something like this:

x = OpenSSL::Digest::SHA512.new('test').digest
# "\xEE&\xB0\xDDJ\xF7\xE7I\xAA\x1A\x8E\xE3\xC1\n\xE9\x92?a\x89\x80w.G?\x88\x19\xA5\xD4\x94\x0E\r\xB2z\xC1\x85\xF8\xA0\xE1\xD5\xF8O\x88\xBC\x88\x7F\xD6{\x1472\xC3\x04\xCC_\xA9\xAD\x8EoW\xF5\x00(\xA8\xFF"

how to convert x to BigDecimal, or more specific DataMapper::Property::Decimal? so i can search it

class MyRecord
  include DataMapper::Resource
  property :id, Serial
  property :amount, Decimal, precision: 156
end

DataMapper.finalize
DataMapper.auto_migrate!

x = OpenSSL::Digest::SHA512.new('test').digest
x = # convert to BigDecimal or DataMapper::Property::Decimal

y = MyRecord.new
y.amount = x
y.save 

z = MyRecord.first(amount: x)

or, if there is no shortcut, what is the correct type to store those values? since PostgreSQL's string cannot store NULL characters

Это было полезно?

Решение

I would try DataMapper's Binary type:

class MyRecord
  include DataMapper::Resource
  property :id, Serial
  property :amount, Binary, length: 64
end

Another approach would be to base64 encode the digest and store it as a string:

class MyRecord
  include DataMapper::Resource
  property :id, Serial
  property :amount, String, length: 88
end

and then:

require 'base64'

x = OpenSSL::Digest::SHA512.new('test').digest
x = Base64.strict_encode64(x)

y = MyRecord.new
y.amount = x
y.save
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top