What does User.destroy_all or User.delete_all do?

I am working on a project that has the following cucumber step:

Given /^no registered users$/ do
  User.delete_all
end

As a new RoR user, this looks a little dangerous, although I would test our development database because our table Userhas actual data. What does a line of code do?

Thank!

+3
source share
4 answers

delete_all from the activerecord library not from FactoryGirl.

And the difference between the two:

delete_all (conditions = nil) public

  • Deletes conditions matching the conditions of the record without instantiating the records, and therefore does not call the destroy method and does not call callbacks.
  • This is one SQL DELETE statement that goes directly to the database, much more efficient than destroy_all.
  • , , : , .
  • .

destroy_all ( = nil) public

  • , .
  • ( : __/_).
  • , ; , , ( ).

, , . SQL DELETE . , , delete_all.

+7

delete_all FactoryGirl, , . , , .

destroy_all, . , , before_destroy , .

, delete_all

+2

delete_all , .

destroy_all ,

+1

, , , Cucumber . ActiveRecord:: Base # delete_all , :

, , , destroy . SQL DELETE, , , destroy_all.

, , , , .

Since this is dangerous, your tests should run against a test database, not development or production databases. Since the test structure can be incorrectly configured for the wrong database, you can certainly add a step or condition if the tests, if Rails.env.test?true. This is a pretty small price to pay for peace of mind.

0
source