我知道在serialize
中搜索不好,但是我遇到了這個問題。在rails中搜索序列化列
我得到了一個叫做問題模型,幷包含一個序列化列assignments
id question_set_id assignments
1 1 {"12982": true, "12332": true}
2 2 {"12222": true, "12332": true}
3 3 {'11111': true}
而且我得到了一個陣列稱爲group_ids
group_ids = ["12982","12332"]
我需要找到誰包含至少一個group_id
記錄assignments
。
所以,這個例子的結果應該是像
[
{
:id => 1,
:question_set_id => 1,
:assignments => {"12982": true, "12332": true}
},
{
:id => 2,
:question_set_id => 2,
:assignments => {"12222": true, "12332": true}
}
]
我已經試過
Question.where("assignments IS NOT NULL").where("assignments LIKE '%?%'", 12982)
這似乎工作,但如何應用一個數組?
而且根據這個answer,我試過
Question.where("assignments IS NOT NULL").where("assignments= ?", groups_ids.to_yaml)
但是,它返回一個空數組。
你使用哪個數據庫(postrgesql/mysql)?你使用哪個序列化器(JSON,Hash,Custom)?哪種類型是您的序列化列(文本,JSON,hstore)? – slowjack2k
mysql,JSON和文本 –