Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过ActiveRecord访问序列化哈希_Ruby On Rails_Ruby_Activerecord_Serialization_Hash - Fatal编程技术网

Ruby on rails 通过ActiveRecord访问序列化哈希

Ruby on rails 通过ActiveRecord访问序列化哈希,ruby-on-rails,ruby,activerecord,serialization,hash,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] MyModel.where("foo = ? AND times[:weekday] IS NOT NULL", baz) 但我在尝试以下操作时遇

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

class MyModel < ActiveRecord::Base
    serialize :times, Hash
end
# write
my_model.times[:weekday] = "foo"

# read
bar = my_model.times[:weekend]
MyModel.where("foo = ? AND times[:weekday] IS NOT NULL", baz)
但我在尝试以下操作时遇到了大约
次[:weekday]
的语法错误:

class MyModel < ActiveRecord::Base
    serialize :times, Hash
end
# write
my_model.times[:weekday] = "foo"

# read
bar = my_model.times[:weekend]
MyModel.where("foo = ? AND times[:weekday] IS NOT NULL", baz)
我在这里遗漏了什么?

关于:

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

您的数据库不知道times列实际上是一个散列,只有Ruby on Rails知道它。YouDB将此字段视为字符串。您不能在序列化列上设置条件,只有空测试(因为空数组不会被认为是空的,所以这永远不会是真的)我想您的意思是
。选择{m | m.times[:weekday]。present?}