Ruby on rails 使用Rails设备生成NestedHstore散列
我有一个名为Ruby on rails 使用Rails设备生成NestedHstore散列,ruby-on-rails,ruby-on-rails-4,capybara,puma,hstore,Ruby On Rails,Ruby On Rails 4,Capybara,Puma,Hstore,我有一个名为custom的字段,它被定义为NestedHstore: # project.rb serialize :custom, ActiveRecord::Coders::NestedHstore 我为该模型创建夹具以进行测试: one: company: one user: one custom: :name: :prompt: 'What is your name?' 运行此操作时,嵌套哈希将保存为字符串,即@project.custom={“nam
custom
的字段,它被定义为NestedHstore:
# project.rb
serialize :custom, ActiveRecord::Coders::NestedHstore
我为该模型创建夹具以进行测试:
one:
company: one
user: one
custom:
:name:
:prompt: 'What is your name?'
运行此操作时,嵌套哈希将保存为字符串,即@project.custom={“name”=>“{:prompt=>\“您的名字是什么?\”}
在单元测试和功能测试中,我通过添加一个prepare\u custom
方法绕过了这个问题,该方法在任何操作运行之前从字符串中手动重建嵌套哈希
def prepare_custom(p)
if p.custom.present?
if p.custom.is_a? Hash
p.custom.each do |k,v|
if (v.class == String) && (v[0] == "{") && (v[-1] == "}")
p.custom[k] = eval(v)
end
end
end
p.save
p.reload
puts "*** project: #{p.inspect}"
end
end
当我运行集成测试(使用Capybara/Poltergeist/Puma)时,会发生以下情况:
prepare\u custom(@project)
似乎起作用了<代码>p.inspect结尾显示custom是一个正确嵌套的散列prepare\u custom
之后立即运行的集成测试代码似乎完全忽略了项目已经更改。它认为p.custom[:name]
又是一个字符串prepare\u custom
中完成的工作
随访 在夹具中尝试:
custom:
后,我们得到:
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::InternalError: ERROR: Syntax error near ':' at position 4
LINE 1: ...company_id") VALUES ('Simple prompt', 0, '---
^
: INSERT INTO "projects" ("name", "custom", "created_at", "updated_at", "id", "company_id") VALUES ('Simple prompt', '---
:name:
:prompt: What is your name?
', '2017-05-17 21:15:25', '2017-05-17 21:15:25', 159160234, 980190962, 159160234)
在您的设备中尝试
自定义:
file@ThomasWalpole我得到:ActiveRecord::StatementInvalid:ActiveRecord::StatementInvalid:PG::InternalError:ERROR:Syntax ERROR靠近“:”的位置4
——其余的都添加到了问题中。@sCirrus你找到解决方案了吗?我自己也面临这个问题。请在您的设备中尝试custom:
file@ThomasWalpole我得到:ActiveRecord::StatementInvalid:ActiveRecord::StatementInvalid:PG::InternalError:ERROR:Syntax ERROR靠近“:”的位置4
——其余的都添加到了问题中。@sCirrus你找到解决方案了吗?我自己面对这个问题。