2012-11-10 25 views
0

我是rails新手。我有一個名爲OfflineExport的模型。在表中我有這樣ActiveRecord where子句錯誤

#<OfflineExport id: 2, 
    parameters: 
     {"project_id"=>"3", 
     "type"=>"submissions", 
     "filters"=>{"task_type"=>"", 
     "corrections"=>"", "grade"=>"", 
     "min_duration"=>"", "after"=>"", 
     "max_duration"=>"", "reviews"=>"", 
     "before"=>""}, 
    "send_email"=>"true", 
    "options"=>{"offline_record_id"=>2}}> 

調幅數據試圖獲取參數[ 「PROJECT_ID」]在where子句像

OfflineExport.where("parameters[project_id] = '3'") 

但我得到的錯誤,如:

ActiveRecord::StatementInvalid: PGError: ERROR: cannot subscript type text because it is not an array 

能有人幫我解決這個問題嗎?

回答

0

看來你的模型中有一個序列化的列。像這樣:serialize :parameters。這意味着數據不會以易於閱讀的格式存儲在數據庫中,這會導致您無法在其上查詢。 Same as here

解決方案:提取您想要查詢的字段,併爲其創建一個列。

+0

所以,你的意思是我必須使用「MyModel.all.select {| m | m.mycode.include?43402}」現在這樣嗎? –

+0

不是。一個類似的解決方案可以工作(這顯然不是爲你的模式編寫的),但是這會大大減慢你的程序。您應該爲該數據創建一個新列,然後對其進行查詢。 – Matzi

+0

hmm..ok ..感謝一個聲音:) –