从ActiveRecord格式创建一个对象

huangapple go评论67阅读模式
英文:

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.

[#&lt;Payment id: 1400, payment_number: &quot;###&quot;, user_id: ###, project_id: 85, phase_id: 155, amount: &quot;500&quot;, notes: nil, status: &quot;complete&quot;, created_at: &quot;2023-03-16 16:50:23.434401000 -0400&quot;, updated_at: &quot;2023-03-16 16:50:23.447559000 -0400&quot;, description: &quot;###&quot;, processed_at: nil, payout_category: &quot;manual&quot;&gt;, #&lt;Payment id: 1402, payment_number: &quot;###&quot;, user_id: ###, project_id: nil, phase_id: nil, amount: nil, notes: &quot;###&quot;, status: &quot;incomplete&quot;, created_at: &quot;2023-03-17 03:02:56.445550000 -0400&quot;, updated_at: &quot;2023-03-20 11:42:56.153214000 -0400&quot;, description: &quot;###&quot;, processed_at: nil, payout_category: &quot;hyperwallet&quot;&gt;]

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)

huangapple
  • 本文由 发表于 2023年3月21日 01:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定