How do I delete all records in one of my database tables in a Ruby on Rails app?
如何删除我在Ruby on Rails应用程序中的一个数据库表中的所有记录?
7 个解决方案
#1
211
If you are looking for a way to it without SQL you should be able to use delete_all.
如果您正在寻找一种不使用SQL的方法,那么应该能够使用delete_all。
Post.delete_all
or with a criteria
或标准
Post.delete_all "person_id = 5 AND (category = 'Something' OR category = 'Else')"
See here for more information.
更多信息请看这里。
The records are deleted without loading them first which makes it very fast but will break functionality like counter cache that depends on rails code to be executed upon deletion.
记录被删除而没有首先加载它们,这使得它非常快速,但是会破坏计数器缓存之类的功能,因为计数器缓存依赖于删除时执行的rails代码。
#2
24
To delete via SQL
通过SQL删除
Item.delete_all # accepts optional conditions
delete_all #接受可选条件
To delete by calling each model's destroy method (expensive but ensures callbacks are called)
通过调用每个模型的销毁方法来删除(代价高昂,但确保调用回调)
Item.destroy_all # accepts optional conditions
驱逐舰_all #接受可选条件
All here
都在这里
#3
18
if you want to completely empty the database and not just delete a model or models attached to it you can do:
如果你想完全清空数据库,而不只是删除一个模型或模型,你可以这样做:
rake db:purge
you can also do it on the test database
您也可以在测试数据库上执行
rake db:test:purge
#4
4
BlogPost.find_each(&:destroy)
#5
4
If you mean delete every instance of all models, I would use
如果您的意思是删除所有模型的每个实例,我将使用
ActiveRecord::Base.connection.tables.map(&:classify)
.map{|name| name.constantize if Object.const_defined?(name)}
.compact.each(&:delete_all)
#6
2
More recent answer in the case you want to delete every entries in every tables:
如果你想要删除每个表中的每个条目,请给出最近的答案:
def reset
Rails.application.eager_load!
ActiveRecord::Base.descendants.each { |c| c.delete_all unless c == ActiveRecord::SchemaMigration }
end
More information about the eager_load
here.
关于eager_load的更多信息。
After calling it, we can access to all of the descendants of ActiveRecord::Base
and we can apply a delete_all
on all the models.
在调用它之后,我们可以访问ActiveRecord: Base的所有后代,并可以在所有模型上应用delete_all。
Note that we make sure not to clear the SchemaMigration table.
注意,我们确保不清除SchemaMigration表。
#7
0
If your model is called BlogPost, it would be:
如果你的模型被称为BlogPost,它将是:
BlogPost.all.map(&:destroy)
#1
211
If you are looking for a way to it without SQL you should be able to use delete_all.
如果您正在寻找一种不使用SQL的方法,那么应该能够使用delete_all。
Post.delete_all
or with a criteria
或标准
Post.delete_all "person_id = 5 AND (category = 'Something' OR category = 'Else')"
See here for more information.
更多信息请看这里。
The records are deleted without loading them first which makes it very fast but will break functionality like counter cache that depends on rails code to be executed upon deletion.
记录被删除而没有首先加载它们,这使得它非常快速,但是会破坏计数器缓存之类的功能,因为计数器缓存依赖于删除时执行的rails代码。
#2
24
To delete via SQL
通过SQL删除
Item.delete_all # accepts optional conditions
delete_all #接受可选条件
To delete by calling each model's destroy method (expensive but ensures callbacks are called)
通过调用每个模型的销毁方法来删除(代价高昂,但确保调用回调)
Item.destroy_all # accepts optional conditions
驱逐舰_all #接受可选条件
All here
都在这里
#3
18
if you want to completely empty the database and not just delete a model or models attached to it you can do:
如果你想完全清空数据库,而不只是删除一个模型或模型,你可以这样做:
rake db:purge
you can also do it on the test database
您也可以在测试数据库上执行
rake db:test:purge
#4
4
BlogPost.find_each(&:destroy)
#5
4
If you mean delete every instance of all models, I would use
如果您的意思是删除所有模型的每个实例,我将使用
ActiveRecord::Base.connection.tables.map(&:classify)
.map{|name| name.constantize if Object.const_defined?(name)}
.compact.each(&:delete_all)
#6
2
More recent answer in the case you want to delete every entries in every tables:
如果你想要删除每个表中的每个条目,请给出最近的答案:
def reset
Rails.application.eager_load!
ActiveRecord::Base.descendants.each { |c| c.delete_all unless c == ActiveRecord::SchemaMigration }
end
More information about the eager_load
here.
关于eager_load的更多信息。
After calling it, we can access to all of the descendants of ActiveRecord::Base
and we can apply a delete_all
on all the models.
在调用它之后,我们可以访问ActiveRecord: Base的所有后代,并可以在所有模型上应用delete_all。
Note that we make sure not to clear the SchemaMigration table.
注意,我们确保不清除SchemaMigration表。
#7
0
If your model is called BlogPost, it would be:
如果你的模型被称为BlogPost,它将是:
BlogPost.all.map(&:destroy)