英文:
Create an object from ActiveRecord format
问题
In trying to debug an issue that's appearing in production, I would like to copy some of the live data into my local database.
要调试出现在生产环境中的问题,我想将一些实时数据复制到我的本地数据库。
To begin with, I'm just trying to copy the latest two records.
首先,我只是尝试复制最新的两条记录。
payments = Payment.all[-2..-1]
支付 = Payment.all[-2..-1]
Here is the return. I've censored most of the info for client and customer privacy.
以下是返回结果。出于客户和客户隐私的考虑,我对大部分信息进行了审查。
[#<Payment id: 1400, payment_number: "###", user_id: ###, project_id: 85, phase_id: 155, amount: "500", notes: nil, status: "complete", created_at: "2023-03-16 16:50:23.434401000 -0400", updated_at: "2023-03-16 16:50:23.447559000 -0400", description: "###", processed_at: nil, payout_category: "manual">, #<Payment id: 1402, payment_number: "###", user_id: ###, project_id: nil, phase_id: nil, amount: nil, notes: "###", status: "incomplete", created_at: "2023-03-17 03:02:56.445550000 -0400", updated_at: "2023-03-20 11:42:56.153214000 -0400", description: "###", processed_at: nil, payout_category: "hyperwallet">]
这是返回结果。出于客户和客户隐私的考虑,我已对大部分信息进行了审查。
In the past, I've successfully copied records by manually pasting the data into a notepad. Then I replace the pointy braces "<>" with curly brackets "{}", and delete the id attribute. This creates a hash that I can iterate across to populate a new record.
过去,我曾成功地通过将数据手动粘贴到记事本中来复制记录。然后,我将尖括号“<>”替换为花括号“{}”,并删除id属性。这将创建一个哈希,我可以遍历以填充新记录。
It seems like Rails would have a better way to read such a record in this format, since that's the way it spits them out. Is there a less time-consuming and error-prone way to create records in the console when given input in ActiveRecord format?
看起来Rails可能有更好的方法来读取这种格式的记录,因为这是它输出记录的方式。在给定ActiveRecord格式的输入时,是否有一种更省时且不容易出错的方式来在控制台中创建记录?
Thanks in advance for any insight.
感谢提前提供的任何见解。
英文:
In trying to debug an issue that's appearing in production, I would like to copy some of the live data into my local database.
To begin with, I'm just trying to copy the latest two records.
payments = Payment.all[-2..-1]
Here is the return. I've censored most of the info for client and customer privacy.
[#<Payment id: 1400, payment_number: "###", user_id: ###, project_id: 85, phase_id: 155, amount: "500", notes: nil, status: "complete", created_at: "2023-03-16 16:50:23.434401000 -0400", updated_at: "2023-03-16 16:50:23.447559000 -0400", description: "###", processed_at: nil, payout_category: "manual">, #<Payment id: 1402, payment_number: "###", user_id: ###, project_id: nil, phase_id: nil, amount: nil, notes: "###", status: "incomplete", created_at: "2023-03-17 03:02:56.445550000 -0400", updated_at: "2023-03-20 11:42:56.153214000 -0400", description: "###", processed_at: nil, payout_category: "hyperwallet">]
In the past, I've successfully copied records by manually pasting the data into a notepad. Then I replace the pointy braces "<>" with curly brackets "{}", and delete the id attribute. This creates a hash that I can iterate across to populate a new record.
It seems like Rails would have a better way to read such a record in this format, since that's the way it spits them out. Is there a less time-consuming and error-prone way to create records in the console when given input in ActiveRecord format?
Thanks in advance for any insight.
答案1
得分: 1
你可以使用.dup
来复制具有空id的记录,然后使用.upsert_all
来插入它们。如果你在database.yml中设置了两个数据库连接,那么你可以使用该引用来连接它们。
ActiveRecord::Base.establish_connection(:productions)
dupes = Payment.all[-2..-1].map {|p| p.dup}
ActiveRecord::Base.establish_connection(:development)
Payment.upsert_all(dupes)
英文:
You could use .dup
to make copies of the records with nil ids, then .upsert_all
to insert them. If you have both DB connections setup in your database.yml then you can use that reference for the connections.
ActiveRecord::Base.establish_connection(:productions)
dupes = Payment.all[-2..-1].map {|p| p.dup}
ActiveRecord::Base.establish_connection(:development)
Payment.upsert_all(dupes)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论