12月21日 简单理解Active Recore Callback, destroy_all和delete_all的区别。豆知识(alias),语言学习法(4核心)
destroy_all and delete_all
Destroy the records by instantiating each record and calling its #destroy method.在删除前实例化每条记录,并回调callback execution.
⚠️ :如果想要快速删除,如果这个动作不涉及associations or callbacks, use delete_all instead. 因为delete_all直接使用SQL DELETE .
豆知识:
Transaction: the process of doing business.
在iterm中输入alias,可以显示所有设置的别名:例子ll的别名是“ls -lh”显示详细的文件内容
语言学习法:
1. 用身体记忆(肌肉记忆,第一步是反复做,而不是试图理解背后的逻辑。学习语言包括配合肢体动作)
2. 跟读。(模仿,纠正)
3. 实际环境中使用。(编程就是直接开发项目,边开发边学。语言就是用,说起来。)
4. 不要被语言本身所限制,语言的目的是沟通,只要符合沟通的目的,可以灵活使用。
Active Record Callbacks:简单学一下:
Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object state.
总共有19种回叫。 如 before_validation, before_create, before_destroy.
继续看api文档:http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
callback可以使用类的继承关系, 当子类的一个before_destroy发生时,也会调用父类的before_destroy.
callback有四种method的写法:symbol, callbackobject, inline method(using a process),还有一种不推荐的忽略。
The method reference callbacks work by specifying a protected or private method available in the object, like this(:symbol):
class Topic < ActiveRecord::Base
before_destroy :delete_parents
private
def delete_parents
self.class.delete_all "parent_id = #{id}"
end
end
callback-object:
before_save EncryptionWrapper.new
after_save EncryptionWrapper.new
Ordering callbacks:
有时候,代码在callbacks执行中需要有特别的顺序。否则会报告❌; 需要使用prepend: true
因为关联的关系的回调和删除是首先执行的,之后才会执行before_destroy.
class Topic < ActiveRecord::Base
has_many :children, dependent: :destroy
before_destroy :log_children, prepend: true
private
def log_children
# Child processing
end
end
Also,there are cases when you want several callbacks of the same type to be executed in order.
如果回调的类型一样,按照代码行顺序执行 .
特例: after_commit,是反过来,先执行最后的method.
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html 文档包括如何debugging callbacks.
12月21日 简单理解Active Recore Callback, destroy_all和delete_all的区别。豆知识(alias),语言学习法(4核心)的更多相关文章
- 2016年12月21日 星期三 --出埃及记 Exodus 21:16
2016年12月21日 星期三 --出埃及记 Exodus 21:16 "Anyone who kidnaps another and either sells him or still h ...
- 【12月21日】A股滚动市盈率PE历史新低排名
2010年01月01日 到 2018年12月21日 之间,滚动市盈率历史新低排名.上市三年以上的公司,2018年12月21日市盈率在300以下的公司. 1 - 厦门象屿(SH600057) - 历史新 ...
- 北京Uber优步司机奖励政策(12月21日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 武汉Uber优步司机奖励政策(12月21日-12.27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 天津Uber优步司机奖励政策(12月21日到12月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 长沙Uber优步司机奖励政策(12月21日到12月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 厦门Uber优步司机奖励政策(12月21日-12.27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 佛山Uber优步司机奖励政策(12月21日到12月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 广州Uber优步司机奖励政策(12月21日到12月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
随机推荐
- Qt的四个常见的图像叠加模式
Qt的QPainter::CompositionMode提供了多种图像叠加的模式.常见的有QPainter::CompositionMode_SourceOver, QPainter::Composi ...
- 20145321 《网络对抗》 Web基础
20145321 <网络对抗> Web基础 基础问题回答 (1)什么是表单 表单在网页中主要负责数据采集功能,一个表单有三个基本组成部分:表单标签——这里面包含了处理表单数据所用CGI程序 ...
- CodeForces 471D MUH and Cube Walls -KMP
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of ...
- my normal Header
#ifndef INCLUDES_MY #define INCLUDES_MY //默认登录名密码 #define DEFAULT_USERNAME "admin" #define ...
- Python3基础 time 索引值访问元组中的年月日时分秒
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3基础 getattr 获取对象的指定属性值
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- Type.Missing和System.Reflection.Missing.Value
Type.Missing https://msdn.microsoft.com/en-us/library/system.type.missing(v=vs.110).aspx Missing.Val ...
- .NET MVC请求流程
ASP.NET MVC 请求流程:Controller MvcHandler Action Action参数赋值 .NET MVC权限设计思考之切入点
- java项目中.classpath,.settings,.project,mymetadata文件的作用
今天犯了一个错误,误修改了本地的.classpath文件,导致项目好多地方报错,之前也没有仔细的研究过项目中的一些生成文件的作用. 今天特此进行记录. 不管我们在eclipse中新建任何的Java项目 ...