开发者

Deleting all records in a database table

开发者 https://www.devze.com 2023-02-17 06:57 出处:网络
How do I delete all records in one of my database tables in a Ruby on Rails ap开发者_开发知识库p?If you are looking for a way to it without SQL you should be able to use delete_all.

How do I delete all records in one of my database tables in a Ruby on Rails ap开发者_开发知识库p?


If you are looking for a way to it without SQL you should be able to use 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.


To delete via SQL

Item.delete_all # accepts optional conditions

To delete by calling each model's destroy method (expensive but ensures callbacks are called)

Item.destroy_all # accepts optional conditions

All here


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


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)


BlogPost.find_each(&:destroy)


If your model is called BlogPost, it would be:

BlogPost.all.map(&:destroy)


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.

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.

Note that we make sure not to clear the SchemaMigration table.


If you have model with relations, you need to destroy models that are related as well.


The fastest way to achieve this:-

  1. Want to delete all data from the table
Post.delete_all
  1. Want to delete specific data from the table, then the right way to do it is
Post.where(YOUR CONDITIONS).delete_all
# this above solution is working in Rails 5.2.1, delete_all don't expect any parameter
# you can let me know if this works in different versions.

# In the older version, you might need to do something like this:-
Post.delete_all "Your Conditions"


This way worked for me, added this route below in routes.rb

get 'del_all', to: 'items#del_all' # del_all is my custom action and items is it's controller

def del_all #action in ItemsController
 if Item.any? 
  Item.destroy_all
  redirect_to items_url, notice: "Items were destroyed." 
 else  
  redirect_to items_url, notice: "No item found here." 
 end     
end

According to documentation:

2.5 Singular Resources - Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the >profile of the currently logged in user. In this case, you can use a singular >resource to map /profile (rather than /profile/:id) to the show action: get 'profile', to: 'users#show'

0

精彩评论

暂无评论...
验证码 换一张
取 消