I know this is probably on the Internet somewhere but I can't find the answer here on * so I thought I may boost up the knowledge base here a little.
我知道这可能是在网上的某个地方,但是我在*上找不到答案,所以我想我可以在这里增加一点知识基础。
I'm a newbie to Ruby and Rails but my company is getting pretty invested in it so I'm trying to get to know it in a little more detail.
我是Ruby和Rails的新手,但是我的公司在这方面投入了大量的资金,所以我正试图更详细地了解它。
It's been difficult for me to change my mindset to designing an application from the "model" rather the from the database, so I'm trying to figure out how would do all of the design work that I have classically done in the Database in the Rails model instead.
对我来说,很难从“模型”而不是数据库中改变我的想法来设计一个应用程序,所以我想弄清楚如何在Rails模型的数据库中完成所有经典的设计工作。
So the most recent task that I have given myself is to figure out how to configure a Rails database model to do cascading deletes? Is there an easy way of doing this? Or would I have to go into the MySql and set this up?
我最近给自己的任务是如何配置Rails数据库模型来执行级联删除?有简单的方法吗?或者我需要进入MySql设置这个吗?
Thanks.
谢谢。
-Matt
马特
5 个解决方案
#1
82
you can also set the :dependent option to :delete_all. :delete_all will issue a single SQL statement to delete all child records. because of this using :delete_all may give you better performance.
还可以将:dependent选项设置为:delete_all。:delete_all将发出一条SQL语句来删除所有子记录。因此,使用:delete_all可以获得更好的性能。
has_many :memberships, dependent: :delete_all
#2
60
Yeah you can, if you are using a relationship like has_many you just do this
是的,你可以,如果你使用has_many这样的关系,你就这么做
has_many :memberships, dependent: :destroy
#3
10
Contrary to the provided answer I highly suggest also doing this on a database level. In case you have different processes or a multi threaded environment it could happen that records are not properly deleted. Furthermore the database foreign key makes things way faster when deleting lots of data.
与提供的答案相反,我强烈建议在数据库级别上也这样做。如果您有不同的进程或多线程环境,可能会出现记录没有被正确删除的情况。此外,数据库外键在删除大量数据时速度更快。
Like in the suggested answer do this:
就像建议的答案一样:
has_many :memberships, dependent: :delete_all
However also make sure to setup a foreign_key
in a migration. That way the database takes care of deleting the records automatically for you.
但是也要确保在迁移中设置一个foreign_key。这样,数据库会自动为您删除记录。
To nullify the values when a membership is deleted, assuming you have a user model:
如果您有一个用户模型,则在删除成员时取消该值:
add_foreign_key :users, :memberships, on_delete: :nullify
You can also delete all the models whenever a membership is deleted
当成员被删除时,您也可以删除所有的模型。
add_foreign_key :users, :memberships, on_delete: :cascade
#4
9
Just keep in mind that delete_all will not execute any callbacks (like before_destroy and after_destroy) on the child records.
请记住,delete_all不会在子记录上执行任何回调(比如before_destroy和after_destroy)。
#5
6
It looks like this plugin might give you what you're looking for if you want the cascading deletes reflected in the actual database structure:
如果你想要在实际的数据库结构中反映级联删除,这个插件可能会提供你想要的东西:
http://www.redhillonrails.org/foreign_key_migrations.html
http://www.redhillonrails.org/foreign_key_migrations.html
Format for using this in a migration would be something like this:
在迁移中使用这个的格式是这样的:
create_table :orders do |t|
t.column :customer_id, :integer, :on_delete => :set_null, :on_update => :cascade
...
end
#1
82
you can also set the :dependent option to :delete_all. :delete_all will issue a single SQL statement to delete all child records. because of this using :delete_all may give you better performance.
还可以将:dependent选项设置为:delete_all。:delete_all将发出一条SQL语句来删除所有子记录。因此,使用:delete_all可以获得更好的性能。
has_many :memberships, dependent: :delete_all
#2
60
Yeah you can, if you are using a relationship like has_many you just do this
是的,你可以,如果你使用has_many这样的关系,你就这么做
has_many :memberships, dependent: :destroy
#3
10
Contrary to the provided answer I highly suggest also doing this on a database level. In case you have different processes or a multi threaded environment it could happen that records are not properly deleted. Furthermore the database foreign key makes things way faster when deleting lots of data.
与提供的答案相反,我强烈建议在数据库级别上也这样做。如果您有不同的进程或多线程环境,可能会出现记录没有被正确删除的情况。此外,数据库外键在删除大量数据时速度更快。
Like in the suggested answer do this:
就像建议的答案一样:
has_many :memberships, dependent: :delete_all
However also make sure to setup a foreign_key
in a migration. That way the database takes care of deleting the records automatically for you.
但是也要确保在迁移中设置一个foreign_key。这样,数据库会自动为您删除记录。
To nullify the values when a membership is deleted, assuming you have a user model:
如果您有一个用户模型,则在删除成员时取消该值:
add_foreign_key :users, :memberships, on_delete: :nullify
You can also delete all the models whenever a membership is deleted
当成员被删除时,您也可以删除所有的模型。
add_foreign_key :users, :memberships, on_delete: :cascade
#4
9
Just keep in mind that delete_all will not execute any callbacks (like before_destroy and after_destroy) on the child records.
请记住,delete_all不会在子记录上执行任何回调(比如before_destroy和after_destroy)。
#5
6
It looks like this plugin might give you what you're looking for if you want the cascading deletes reflected in the actual database structure:
如果你想要在实际的数据库结构中反映级联删除,这个插件可能会提供你想要的东西:
http://www.redhillonrails.org/foreign_key_migrations.html
http://www.redhillonrails.org/foreign_key_migrations.html
Format for using this in a migration would be something like this:
在迁移中使用这个的格式是这样的:
create_table :orders do |t|
t.column :customer_id, :integer, :on_delete => :set_null, :on_update => :cascade
...
end