rails dependent

dependent
可以設定當物件刪除時,也會順便刪除它的 has_many 物件:
class Event < ActiveRecord::Base
has_many :attendees, :dependent => :destroy
end
:dependent 可以有三種不同的刪除方式,分別是:
? :destroy 會執行 attendee 的 destroy 回呼
? :delete 不會執行 attendee 的 destroy 回呼
? :nullify 這是預設值,不會幫忙刪除 attendee
要不要執行 attendee 的刪除回呼效率相差不少,如果需要的話,必須一筆筆把 attendee
讀取出來變成 attendee 物件,然後呼叫它的 destroy。如果用:delete 的話,只需要一
個 SQL 語句就可以刪除全部 attendee。

delete

destroy


destroy是先查出来再删除

2.2.4 :017 > Credit.first.destroy
  Credit Load (0.5ms)  SELECT  "credits".* FROM "credits"  ORDER BY "credits"."id" ASC LIMIT 1
   (0.3ms)  begin transaction
  SQL (0.5ms)  DELETE FROM "credits" WHERE "credits"."id" = ?  [["id", 2]]
   (63.3ms)  commit transa

2.2.4 :006 > Credit.destroy_all
  Credit Load (0.6ms)  SELECT "credits".* FROM "credits"
   (0.1ms)  begin transaction
  SQL (0.4ms)  DELETE FROM "credits" WHERE "credits"."id" = ?  [["id", 7]]
   (59.9ms)  commit transaction
   (0.2ms)  begin transaction
  SQL (0.2ms)  DELETE FROM "credits" WHERE "credits"."id" = ?  [["id", 8]]
   (53.4ms)  commit transaction
   (0.2ms)  begin transaction
  SQL (0.3ms)  DELETE FROM "credits" WHERE "credits"."id" = ?  [["id", 9]]
   (53.7ms)  commit transaction

2.2.4 :023 > Credit.first.delete
  Credit Load (0.5ms)  SELECT  "credits".* FROM "credits"  ORDER BY "credits"."id" ASC LIMIT 1
  SQL (54.7ms)  DELETE FROM "credits" WHERE "credits"."id" = ?  [["id", 3]]

2.2.4 :005 > Post.delete_all
  SQL (73.7ms)  DELETE FROM "posts"
时间: 2024-11-08 21:49:51

rails dependent的相关文章

rails性能优化

1. 性能优化的重要性 减轻服务器压力(量化起来就是执行时间) 增加使用者的好感(快!) 2. ActiveRecord:表关联 ActiveRecord可以用Associations来定义表之间的关联性.到目前为止我们学会了用ActiveRecord来操作数据库,但是还沒充分发挥关联式数据库的特性,那就是透过primary key和foreign keys将表互相关联起来 Primary Key这个字段在Rails中,照惯例叫做id,类型是整数且递增.而Foreign Key字段照惯例会叫做{

10 steps to get Ruby on Rails running on Windows with IIS FastCGI

Since the original tech preview release of FastCGI last year, we've been seeing a lot of requests for getting Ruby on Rails running with our FastCGI.  Theoretically, since the FastCGI component uses a standard protocol to support FastCGI-enabled appl

rails将类常量重构到数据库对应的表之后记

怎么还有啊!别急,有强迫症的人伤不起!有点小事没说完感觉痒痒的:就是如果表payment_types经常变动该怎么办?每次都要关闭rails网页服务器,然后重启吗?那也太麻烦鸟,最终的解决方案是,在Order的验证中强制每次都从数据库的表中拉取支付类型数据比较,最后修改完的Order.rb代码如下: class Order < ActiveRecord::Base has_many :line_items,dependent: :destroy validates_each :pay_type

RailsCast26 Hackers Love Mass Assignment rails中按params创建、更新model时存在的安全隐患

Mass assignment是rails中常用的将表单数据存储起来的一种方式.不幸的是,它的简洁性成了黑客攻击的目标.下面将解释为什么及如何解决. 上述表单为一个简单的注册表单.当用户填入name,点击提交时,一个新用户被创建.用户模型被如下定义: ruby create_table :users do |t| t.string :name t.boolean :admin, :default => false, :null => false end 当用户点击提交时,如下的action被执

Rails 5 开发进阶

Rails 5 开发进阶:https://www.gitbook.com/book/kelby/rails-beginner-s-guide/details cancan : http://blog.xdite.net/posts/2012/07/30/cancan-rule-engine-authorization-based-library-1/ Ruby官方文档翻译(Ruby官方文档中文版) : http://blog.csdn.net/liuk10/article/details/509

nginx unicorn 来运行rails

一.安装nginx sudo apt-get install nginx 安装完成后查看一下:nginx -v 说明安装成功. ubuntu系统里的安装目录是在/etc/nginx/下,启动程序文件在/usr/sbin/nginx 二.新建项目 rails new app --skip-bundle 完成后修改Gemfile文件:vim Gemfile 把source 修改成taobao或者ruby-china的源. 在这个文件里加入:gem 'unicorn' 然后运行:bundle inst

rails中是如何实现定时任务的之sidetip

环境:ubuntu14.4,ruby2.1.5, rails4.2 sidetip 依赖于sidekiq,所以在Gemfile里要添加sidekiq. 一.新一个rais项目:rails new schedule_job --skip-bundle 进入项目文件夹: cd schedule_job 打开Gemfile文件:vim Gemfile 把source 'https://rubygems.org'改为source 'https://ruby.taobao.org'. 在文件里加入:gem

rails.vim环境安装(ubuntu)

学习和开发Ruby & Rails已经一年多了.但是正式全职Rails项目的开发只有可怜的4个月经验.这期间我一直使用Netbeans 6.x作为开发工具,到目前为止我依然认为它是开发Ruby & Rails程序的最佳开发工具(我没用过最新出来的RubyMine,另外这是一个付费产品,我也不会付费用它,但也不会用D版). 在网上可以看到很到开发Rails的同学使用Vim这个神器来开发的(就在JaveEye也有不少).但是由于我的vim水平只局限于只会使用有限的几个命令( i, :w, :x

how to change the ruby and rails version

changing ruby version checking ruby version run(if you had have install rvm to manage rubies: $rvm list $rvm use ruby-2.0.0-p643 --defualt changing rails version: changing the rails versions when running rails new to build a new app(installing rails