简体   繁体   English

Rails数据库创建重复输入

[英]rails database creating double entry

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. 现在,当我使用rails应用程序在数据库中创建新条目时,它正在创建具有相同信息的两行,并给其中之一加上时间戳,就像使用夹具创建时一样。

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. 如您所见,时间23:40:32是固定装置更新数据库的时间。

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. 请注意最后两组条目,task_id为42和43,它已为procedure_id 1添加了两次,一次是在初始设置期间添加的,另一次是我实际进行的添加的。

These tasks are not in the fixtures, I didn't add them to the database until after the initial load. 这些任务不在固定装置中,我直到初始加载后才将它们添加到数据库中。

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? 同时使用.new和.save创建两个条目? I thought .new didn't save to the database, but after removing the .save it worked fine. 我以为.new不会保存到数据库,但是删除.save后,它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM