通过ActiveRecord访问序列化哈希

时间:2013-09-04 15:45:43

标签: ruby-on-rails ruby activerecord serialization hash

我在MyModel中有一个序列化哈希,如下所示:

class MyModel < ActiveRecord::Base
    serialize :times, Hash
end

我能够写入和读取序列化哈希,如下所示:

# write
my_model.times[:weekday] = "foo"

# read
bar = my_model.times[:weekend]

但我遇到times[:weekday]周围的语法错误,同时尝试这样的事情:

MyModel.where("foo = ? AND times[:weekday] IS NOT NULL", baz)

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

怎么样:

MyModel.where("foo = ?", baz).select { |m| m.weekday.present? }