使用依赖销毁和依赖无效时的Rails

时间:2022-12-22 01:20:47

I want to know about relation dependent destroy and dependent nullify on rails and relation with SQL.

我想知道在rails上依赖关系的销毁和依赖关系的无效,以及与SQL的关系。

Thanks

谢谢

3 个解决方案

#1


2  

Example: Table users and table cars

例如:桌面用户和桌面车

user has many cars car belongs to users

用户拥有许多属于用户的汽车

in table car you have user_id on each row

在表car中,每一行都有user_id

if you set dependent destroy when defining the relationship in users, then when you delete a user, all cars having that user_id will be deleted also

如果在定义用户关系时设置了依赖销毁,那么在删除用户时,所有具有user_id的汽车也将被删除

if you set nullify, cars will remain, but the user_id column will be set to null (it is pointless to have any value there because the user with that id was deleted)

如果设置为nullify, cars将保持不变,但user_id列将被设置为null(没有任何值是没有意义的,因为具有该id的用户已被删除)

Hope that this helps

希望这对您有所帮助

#2


0  

You use these options when you want to get rid of orphaned records. Most common used is destroy because it removes all associated objects one by one.

当您想要删除孤立的记录时,您可以使用这些选项。最常见的用法是销毁,因为它逐个删除所有关联的对象。

#3


0  

You can use dependent when we want to get rid of orphaned records since they can lead to various problems. Orphaned records are created when we delete or destroy a model A that was associated with model B, but model B wasn't removed in the process.

当我们想要删除孤立的记录时,您可以使用dependency,因为它们可能导致各种各样的问题。当我们删除或销毁与模型B关联的模型a时,会创建孤立的记录,但过程中并没有删除模型B。

You most often want to use destroy - all associated objects will remove one by one. other common options are:

您通常希望使用destroy——所有关联的对象将逐个删除。其他常见的选项是:

:delete_all – all associated objects will be deleted in a single query.

:delete_all -所有关联对象将在一个查询中被删除。

:nullify – foreign keys will be set to NULL

:nullify -外键将被设置为NULL

You can check more details about this here.

你可以在这里查看更多细节。

#1


2  

Example: Table users and table cars

例如:桌面用户和桌面车

user has many cars car belongs to users

用户拥有许多属于用户的汽车

in table car you have user_id on each row

在表car中,每一行都有user_id

if you set dependent destroy when defining the relationship in users, then when you delete a user, all cars having that user_id will be deleted also

如果在定义用户关系时设置了依赖销毁,那么在删除用户时,所有具有user_id的汽车也将被删除

if you set nullify, cars will remain, but the user_id column will be set to null (it is pointless to have any value there because the user with that id was deleted)

如果设置为nullify, cars将保持不变,但user_id列将被设置为null(没有任何值是没有意义的,因为具有该id的用户已被删除)

Hope that this helps

希望这对您有所帮助

#2


0  

You use these options when you want to get rid of orphaned records. Most common used is destroy because it removes all associated objects one by one.

当您想要删除孤立的记录时,您可以使用这些选项。最常见的用法是销毁,因为它逐个删除所有关联的对象。

#3


0  

You can use dependent when we want to get rid of orphaned records since they can lead to various problems. Orphaned records are created when we delete or destroy a model A that was associated with model B, but model B wasn't removed in the process.

当我们想要删除孤立的记录时,您可以使用dependency,因为它们可能导致各种各样的问题。当我们删除或销毁与模型B关联的模型a时,会创建孤立的记录,但过程中并没有删除模型B。

You most often want to use destroy - all associated objects will remove one by one. other common options are:

您通常希望使用destroy——所有关联的对象将逐个删除。其他常见的选项是:

:delete_all – all associated objects will be deleted in a single query.

:delete_all -所有关联对象将在一个查询中被删除。

:nullify – foreign keys will be set to NULL

:nullify -外键将被设置为NULL

You can check more details about this here.

你可以在这里查看更多细节。