2009-12-15 77 views
1

這是我試過問題節能軌元帥

f = 1.2 
f = Marshal.dump(f) #\004\bf\v1.2\00033 
後,我試圖挽救該F到文本列

,這是一個錯誤我。

ActiveRecord::StatementInvalid: SQLException: unrecognized token: "fϾ1.2 33" (Ͼ is male symbol, but I can't find one). 
+0

不要忘記標記答案解決 – phoet 2014-11-25 08:22:14

回答

5

我用在我的模型一個簡單的包裝轉儲數據並將其編碼的Base64,以便它是一個原始字符串:

def data=(data) 
    write_attribute :data, ActiveSupport::Base64.encode64(Marshal.dump(data)) 
end 

def data 
    Marshal.load(ActiveSupport::Base64.decode64(read_attribute :data)) 
end 
0

phoet的回答是不錯的,我只加空值的支持所以你加載時不會出錯。

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