Problem saving rails marshal in sqlite3 db
This is what I tried
f = 1.2
f =开发者_JAVA技巧 Marshal.dump(f) #\004\bf\v1.2\00033
after that I tried to save this f into text column and this is an error I got.
ActiveRecord::StatementInvalid: SQLException: unrecognized token: "fϾ1.2 33" (Ͼ is male symbol, but I can't find one).
i use a simple wrapper in my model that dumps the data and encodes it base64 so that it is a raw string:
def data=(data)
write_attribute :data, ActiveSupport::Base64.encode64(Marshal.dump(data))
end
def data
Marshal.load(ActiveSupport::Base64.decode64(read_attribute :data))
end
phoet's answer is good, I only added support for empty values so you don't get error while loading.
def education=(data)
write_attribute :education, ActiveSupport::Base64.encode64(Marshal.dump(data))
end
def education
data = read_attribute :education
if data
Marshal.load(ActiveSupport::Base64.decode64(data))
else
nil
end
end
精彩评论