- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
create_table "drugs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "item_seq"
t.string "name"
t.string "short_description"
t.string "short_notice"
t.json "package_insert"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
我正在尝试更新上面显示的模型 Drug
。它有大约 50,000 行数据,我正在使用 ruby 代码使用从单独的 api 服务器获取的数据来更新模型中的项目。我要更新的列是 package_insert
,它的类型是 json
。我只是想更新 package_insert(BAR_CODE, EDI_CODE)
的某些键。
我遇到的问题是我无法更新多行,因为它会在第一行执行提交后抛出错误。
ruby 代码如下所示。
require 'json'
require 'net/http'
require 'active_support/core_ext/hash'
for i in 1..501
response = Net::HTTP.get_response(URI.parse(ENV["API_SERVER"])).body
hashed_response = Hash.from_xml(response)
hashed_response['response']['body']['items']['item'].each do |item|
#select drug to be updated
drug_to_change = Drug.find_by(item_seq: item['ITEM_SEQ'])
unless drug_to_change.nil?
package = JSON.parse(drug_to_change.package_insert)
package['DRB_ITEM']['BAR_CODE'] = item['BAR_CODE']
if !item['EDI_CODE'].nil?
package['DRB_ITEM']['EDI_CODE'] = item['EDI_CODE']
end
drug_to_change.update(package_insert: package.to_json)
else
new_item = Drug.new
new_item.item_seq = item['ITEM_SEQ']
new_item.name = item['ITEM_NAME']
#update
a = Hash.new
a["DRB_ITEM"] = item
new_item.save
end
end
end
当我运行时
# terminal
bin/rails runner lib/file_name.rb
# result
Running via Spring preloader in process 51929
Traceback (most recent call last):
85: from -e:1:in `<main>'
84: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
83: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
82: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:285:in `load'
81: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
80: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:285:in `block in load'
79: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
78: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
77: from /Users/jamie/Desktop/test_branch/bin/rails:9:in `<main>'
76: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
75: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
74: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
73: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
72: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
71: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
70: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
69: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
68: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands.rb:18:in `<main>'
67: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/command.rb:46:in `invoke'
66: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/command/base.rb:65:in `perform'
65: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
64: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
63: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
62: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands/runner/runner_command.rb:38:in `perform'
61: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
60: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
59: from /Users/jamie/Desktop/test_branch/lib/renew_all.rb:5:in `<main>'
58: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/core_ext/range/each.rb:9:in `each'
57: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/core_ext/range/each.rb:9:in `each'
56: from /Users/jamie/Desktop/test_branch/lib/renew_all.rb:9:in `block in <main>'
55: from /Users/jamie/Desktop/test_branch/lib/renew_all.rb:9:in `each'
54: from /Users/jamie/Desktop/test_branch/lib/renew_all.rb:17:in `block (2 levels) in <main>'
53: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/persistence.rb:424:in `update'
52: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/transactions.rb:385:in `with_transaction_returning_status'
51: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/transactions.rb:212:in `transaction'
50: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:259:in `transaction'
49: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:236:in `within_new_transaction'
48: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
47: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:252:in `block in within_new_transaction'
46: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:213:in `commit_transaction'
45: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
44: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:223:in `block in commit_transaction'
43: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:128:in `commit_records'
42: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/transactions.rb:346:in `committed!'
41: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:816:in `_run_commit_callbacks'
40: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:133:in `run_callbacks'
39: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:517:in `invoke_after'
38: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:517:in `each'
37: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:517:in `block in invoke_after'
36: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:261:in `block in conditional'
35: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:426:in `block in make_lambda'
34: from /Users/jamie/searchkick_forked/lib/searchkick/model.rb:78:in `reindex'
33: from /Users/jamie/searchkick_forked/lib/searchkick/record_indexer.rb:51:in `reindex'
32: from /Users/jamie/searchkick_forked/lib/searchkick/record_indexer.rb:74:in `reindex_record'
31: from /Users/jamie/searchkick_forked/lib/searchkick/logging.rb:27:in `store'
30: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications.rb:168:in `instrument'
29: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
28: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications.rb:168:in `block in instrument'
27: from /Users/jamie/searchkick_forked/lib/searchkick/logging.rb:28:in `block in store'
26: from /Users/jamie/searchkick_forked/lib/searchkick/index.rb:125:in `store'
25: from /Users/jamie/searchkick_forked/lib/searchkick/bulk_indexer.rb:43:in `bulk_index'
24: from /Users/jamie/searchkick_forked/lib/searchkick/indexer.rb:11:in `queue'
23: from /Users/jamie/searchkick_forked/lib/searchkick/logging.rb:126:in `perform'
22: from /Users/jamie/searchkick_forked/lib/searchkick/indexer.rb:18:in `perform'
21: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/bulk.rb:95:in `bulk'
20: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/client.rb:131:in `perform_request'
19: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
18: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/base.rb:266:in `perform_request'
17: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:23:in `block in perform_request'
16: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/faraday-0.15.4/lib/faraday/connection.rb:387:in `run_request'
15: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/faraday-0.15.4/lib/faraday/rack_builder.rb:143:in `build_response'
14: from /Users/jamie/searchkick_forked/lib/searchkick/middleware.rb:9:in `call'
13: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/faraday_middleware-aws-sigv4-0.2.4/lib/faraday_middleware/request/aws_sigv4.rb:15:in `call'
12: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/faraday-0.15.4/lib/faraday/adapter/net_http.rb:38:in `call'
11: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/faraday-0.15.4/lib/faraday/adapter/net_http.rb:92:in `with_net_http_connection'
10: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/faraday-0.15.4/lib/faraday/adapter/net_http.rb:43:in `block in call'
9: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/faraday-0.15.4/lib/faraday/adapter/net_http.rb:87:in `perform_request'
8: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/net/http.rb:1455:in `request'
7: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/net/http.rb:909:in `start'
6: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/net/http.rb:920:in `do_start'
5: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/net/http.rb:935:in `connect'
4: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/timeout.rb:103:in `timeout'
3: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/timeout.rb:93:in `block in timeout'
2: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/net/http.rb:937:in `block in connect'
1: from /Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/net/http.rb:937:in `open'
/Users/jamie/.rbenv/versions/2.5.3/lib/ruby/2.5.0/net/http.rb:937:in `initialize': execution expired (Faraday::ConnectionFailed)
rails 控制台结果中的相同代码:
irb(main):017:0>drug_to_change.update(package_insert: package.to_json)
(17.2ms) BEGIN
(0.3ms) COMMIT
Drug Store (30237.8ms) {"id":1,"exception":["Faraday::ConnectionFailed","execution expired"],"exception_object":"execution expired"}
Traceback (most recent call last):
1: from (irb):17
Faraday::ConnectionFailed (execution expired)
我猜测问题是由于 mysql 提交花费的时间太长。我该如何解决这个问题?提前致谢。
SHOW CREATE TABLE drugs;
最佳答案
错误 Faraday::ConnectionFailed
表明您在更新链(package_insert=
setter、模型验证或回调)中的某处有一个 http 请求失败并出现此错误。事实也证明了这一点,通过控制台进行的简单更新会产生相同的结果。
由于错误是由简单的 obj.update(package_insert: some_json)
重现的,因此它与您提供的脚本没有直接关系。查看您的模型,尤其是 before_*
/after_*
,很可能有一些 after_save
回调会发出 http 请求(根据经验- 来自模型回调的外部请求通常不是一个好主意,除非您确定如何以及为什么这样做)
关于mysql - 更新 JSON 列花费的时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56326593/
最近开始学习MongoDB。今天老师教了我们 mongoexport 命令。在练习时,我遇到了一个典型的问题,包括教练在内的其他同学都没有遇到过。我在我的 Windows 10 机器上使用 Mongo
我是 JSON Schema 的新手,读过什么是 JSON Schema 等等。但我不知道如何将 JSON Schema 链接到 JSON 以针对该 JSON Schema 进行验证。谁能解释一下?
在 xml 中,我可以在另一个 xml 文件中包含一个文件并使用它。如果您的软件从 xml 获取配置文件但没有任何方法来分离配置,如 apache/ngnix(nginx.conf - site-av
我有一个 JSON 对象,其中包含一个本身是 JSON 对象的字符串。我如何反序列化它? 我希望能够做类似的事情: #[derive(Deserialize)] struct B { c: S
考虑以下 JSON { "a": "{\"b\": 12, \"c\": \"test\"}" } 我想定义一个泛型读取 Reads[Outer[T]]对于这种序列化的 Json import
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 11 个月前关闭。 Improve
我的旧项目在 MySQL 中有 Standard JSON 格式的数据。 对于我在 JS (Node.js) 和 DynamoDB 中的全新项目,关于 Standard JSON格式: 是否建议将其转
JSON 值字符串、数字、true、false、null 是否是有效的 JSON? 即,是 true 一个有效的 JSON 文档?还是必须是数组/对象? 一些验证器接受这个(例如 http://jso
我有一个 JSON 字符串,其中一个字段是文本字段。这个文本字段可以包含用户在 UI 中输入的文本,如果他们输入的文本是 JSON 文本,也许是为了说明一些编码,我需要对他们的文本进行编码,以便它不会
我正在通过 IBM MQ 调用处理数据,当由 ColdFusion 10 (10,0,11,285437) 序列化时,0 将作为 +0.0 返回,它会导致无效的 JSON并且无法反序列化。 stPol
我正在从三个数组中生成一个散列,然后尝试构建一个 json。我通过 json object has array 成功了。 require 'json' A = [['A1', 'A2', 'A3'],
我从 API 接收 JSON,响应可以是 30 种类型之一。每种类型都有一组唯一的字段,但所有响应都有一个字段 type 说明它是哪种类型。 我的方法是使用serde .我为每种响应类型创建一个结构并
我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是
我正在尝试根据从 API 接收到的数据动态创建一个 JSON 对象。 收到的示例数据:将数据解码到下面给出的 CiItems 结构中 { "class_name": "test", "
我想从字符串转换为对象。 来自 {"key1": "{\n \"key2\": \"value2\",\n \"key3\": {\n \"key4\": \"value4\"\n }\n
目前我正在使用以下代码将嵌套的 json 转换为扁平化的 json: import ( "fmt" "github.com/nytlabs/gojsonexplode" ) func
我有一个使用来自第三方 API 的数据的应用程序。我需要将 json 解码为一个结构,这需要该结构具有“传入”json 字段的 json 标签。传出的 json 字段具有不同的命名约定,因此我需要不同
我想使用 JSON 架构来验证某些值。我有两个对象,称它们为 trackedItems 和 trackedItemGroups。 trackedItemGroups 是组名称和 trackedItem
考虑以下案例类模式, case class Y (a: String, b: String) case class X (dummy: String, b: Y) 字段b是可选的,我的一些数据集没有字
我正在存储 cat ~/path/to/file/blah | 的输出jq tojson 在一个变量中,稍后在带有 JSON 内容的 curl POST 中使用。它运作良好,但它删除了所有换行符。我知
我是一名优秀的程序员,十分优秀!