2013-03-22 83 views
0

我想添加如下哈希到MySQL列,我可以存儲紅寶石哈希在MySQL數據庫中列

{:sub => ["at","div.product-info"], 
    1 => [["at","span#hs18Price"]], 
    :avail => ["at","strong.out_stock"], 
    :soffer => ["at","div.freebie"], 
    :stime =>["search","div[@class='costs']//span[@class='days']"], 
    :scost => [300,30] } 

我試過了,它拋出下面的錯誤,

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'costs... 

任何幫幫我?

編輯:

忘記的活動記錄,系列化。我想在一個原始的SQL中執行它。

回答

2

你需要逃脫單引號中的散列

+0

它的工作,先生! – beck03076 2013-03-22 10:25:26

0

您的sql列必須爲text。您可能還需要首先序列化您的散列。

1

如果你想存儲哈希裏面列在MySQL(或AR *支持的任何其他數據庫),你需要:

1)將列定義爲文本

add_column :users, :the_whatever, :text 

2)定義列的序列化,以便ActiveRecord知道如何處理您的數據。你可以通過設置序列化選項來建模。

serialize :the_whatever, Hash 

*某些數據庫(如PostgreSQL)還提供了一些更適合存儲Hashes的其他類型。請參閱Railscasts Hstore

1

如果您願意切換數據庫:PostreSQL爲此擁有hstore

+0

Postgres 9.2也具有JSON數據類型。 – 2013-03-22 12:00:23

+0

很酷:-) – 2013-03-22 12:22:50