Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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 使用Rails设备生成NestedHstore散列_Ruby On Rails_Ruby On Rails 4_Capybara_Puma_Hstore - Fatal编程技术网

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]
    又是一个字符串
  • 我怎样才能让fixture正确地保存嵌套哈希,或者让集成测试反映在
    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你找到解决方案了吗?我自己面对这个问题。