Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 751 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete_all vs destroy_all?

#1
I am looking for the best approach to delete records from a table. For instance, I have a user whose user ID is across many tables. I want to delete this user and every record that has his ID in all tables.

u = User.find_by_name('JohnBoy')
u.usage_indexes.destroy_all
u.sources.destroy_all
u.user_stats.destroy_all
u.delete

This works and removes all references of the user from all tables, but I heard that `destroy_all` was very process heavy, so I tried `delete_all`. It only removes the user from his own user table and the `id` from all the other tables are made null, but leaves the records intact in them. Can someone share what the correct process is for performing a task like this?

I see that `destroy_all` calls the `destroy` function on all associated objects but I just want to confirm the correct approach.
Reply

#2
delete_all is a single SQL DELETE statement and nothing more. destroy_all calls destroy() on all matching results of :conditions (if you have one) which could be at least NUM_OF_RESULTS SQL statements.

If you have to do something drastic such as destroy_all() on large dataset, I would probably not do it from the app and handle it manually with care. If the dataset is small enough, you wouldn't hurt as much.
Reply

#3
To avoid the fact that <code>destroy_all</code> instantiates all the records and destroys them one at a time, you can use it directly from the model class.

So instead of :

u = User.find_by_name('JohnBoy')
u.usage_indexes.destroy_all

You can do :

u = User.find_by_name('JohnBoy')
UsageIndex.destroy_all "user_id = #{u.id}"

The result is one query to destroy all the associated records
Reply

#4
You are right. If you want to delete the User and all associated objects -> `destroy_all`
However, if you just want to delete the User without suppressing all associated objects -> `delete_all`

According to this post :

[To see links please register here]


- `destroy` / `destroy_all`: The associated objects are destroyed alongside this object by calling their destroy method
- `delete` / `delete_all`: All associated objects are destroyed immediately without calling their :destroy method
Reply

#5
I’ve made a [small gem](

[To see links please register here]

) that can alleviate the need to manually delete associated records in some circumstances.

>This gem adds a new option for ActiveRecord associations:
>
>dependent: :delete_recursively
>
>When you destroy a record, all records that are associated using this option will be deleted recursively (i.e. across models), without instantiating any of them.
>
>Note that, just like dependent: :delete or dependent: :delete_all, this new option does not trigger the around/before/after_destroy callbacks of the dependent records.
>
>However, it is possible to have dependent: :destroy associations anywhere within a chain of models that are otherwise associated with dependent: :delete_recursively. The :destroy option will work normally anywhere up or down the line, instantiating and destroying all relevant records and thus also triggering their callbacks.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through