Rails 数据库创建复式条目

发布于 2024-09-29 20:14:23 字数 2131 浏览 0 评论 0原文

我正在使用固定装置用一些信息预先填充我的数据库,这样我就可以在测试期间杀死数据库并快速重新创建它。

现在,当我使用 Rails 应用程序在数据库中创建一个新条目时,它会创建具有相同信息的两行,并对其中一行添加时间戳,就像使用固定装置创建的一样。

以下是数据库中的一些行

| task_id | procedure_id | created_at          | updated_at          |
+---------+--------------+---------------------+---------------------+
|      13 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      23 |            5 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       7 |            6 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       3 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       7 |            5 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      27 |            6 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      21 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      21 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      38 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      38 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      37 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       5 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      37 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       5 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      30 |            5 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       3 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      41 |            2 | 2010-11-01 23:44:15 | 2010-11-01 23:44:15 |
|      41 |            3 | 2010-11-01 23:44:15 | 2010-11-01 23:44:15 |
|      41 |            4 | 2010-11-01 23:44:15 | 2010-11-01 23:44:15 |
|      42 |            1 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      42 |            1 | 2010-11-01 23:45:11 | 2010-11-01 23:45:11 |
|      43 |            1 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      43 |            1 | 2010-11-01 23:51:16 | 2010-11-01 23:51:16 |

正如您所看到的,时间 23:40:32 是赛程更新数据库的时间。

请注意最后两组条目,其中 task_id 为 42 和 43,它已为 procedure_id 1 添加了两次,一次显然是在初始设置期间,一次是在我实际执行操作时。

这些任务不在固定装置中,直到初始加载后我才将它们添加到数据库中。

I am using fixtures to pre-populate my database with some information, so I can kill the database and remake it quickly during testing.

Now when I create a new entry in the database using my rails application it is creating two rows with the same information and timestamping one of them as if it were created with the fixture.

here are some lines from the database

| task_id | procedure_id | created_at          | updated_at          |
+---------+--------------+---------------------+---------------------+
|      13 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      23 |            5 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       7 |            6 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       3 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       7 |            5 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      27 |            6 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      21 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      21 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      38 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      38 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      37 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       5 |            8 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      37 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       5 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      30 |            5 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|       3 |            7 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      41 |            2 | 2010-11-01 23:44:15 | 2010-11-01 23:44:15 |
|      41 |            3 | 2010-11-01 23:44:15 | 2010-11-01 23:44:15 |
|      41 |            4 | 2010-11-01 23:44:15 | 2010-11-01 23:44:15 |
|      42 |            1 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      42 |            1 | 2010-11-01 23:45:11 | 2010-11-01 23:45:11 |
|      43 |            1 | 2010-11-01 23:40:32 | 2010-11-01 23:40:32 |
|      43 |            1 | 2010-11-01 23:51:16 | 2010-11-01 23:51:16 |

As you can see the time 23:40:32 was when the fixtures updated the database.

Note the last two sets of entries where task_id is 42 and 43, it has been added for procedure_id 1 twice, once during the initial set up apparently and once when I actually did it.

These tasks are not in the fixtures, I didn't add them to the database until after the initial load.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-10-06 20:14:23
for procedure in @task.procedures
  @procedureTask = ProceduresTask.new()
  @procedureTask.procedure_id = procedure.id
  @procedureTask.task_id = @task.id
  @procedureTask.save
end

应该是:

for procedure in @task.procedures
  @procedureTask = ProceduresTask.new()
  @procedureTask.procedure_id = procedure.id
  @procedureTask.task_id = @task.id
end

同时使用 .new 和 .save 创建两个条目?我以为 .new 没有保存到数据库,但删除 .save 后它工作正常。

for procedure in @task.procedures
  @procedureTask = ProceduresTask.new()
  @procedureTask.procedure_id = procedure.id
  @procedureTask.task_id = @task.id
  @procedureTask.save
end

should be:

for procedure in @task.procedures
  @procedureTask = ProceduresTask.new()
  @procedureTask.procedure_id = procedure.id
  @procedureTask.task_id = @task.id
end

using both .new and .save was creating two entries? I thought .new didn't save to the database, but after removing the .save it worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文