我可以在Rails中设置Cascade删除吗? [英] Can I set up Cascade deleting in Rails?

查看:111
本文介绍了我可以在Rails中设置Cascade删除吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是在互联网上的某个地方,但我找不到这里的答案在Stackoverflow所以我想我可以提高知识库在这里一点。



我是Ruby和Rails的新手,但我的公司正在对它进行投资,所以我想更详细地了解它。



我很难改变我的心态,从模型而不是从数据库设计一个应用程序,所以我想弄清楚如何做所有我在数据库中经典的设计工作。



因此,我给自己最近的任务是找出如何配置一个Rails数据库模型来执行级联删除?有没有一个简单的方法这样做?



-Matt

p>

解决方案

您还可以将:dependent选项设置为:delete_all。 :delete_all将发出单个SQL语句来删除所有子记录。因为这样使用:delete_all可能会给你更好的性能。

  has_many:memberships,:dependent => :delete_all 


I know this is probably on the Internet somewhere but I can't find the answer here on Stackoverflow 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.

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.

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?

Thanks.

-Matt

解决方案

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.

has_many :memberships, :dependent => :delete_all

这篇关于我可以在Rails中设置Cascade删除吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆