ActiveRecord(二)

ActiveRecord 回调

回调

创建对象顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class User < ApplicationRecord
before_validation :beforeValidation
after_validation :afterValidation
before_save :beforeSave
before_create :beforeCreate
after_create :afterCreate
after_save :afterSave



def beforeValidation

end
def afterValidation

end
def beforeSave

end
def beforeCreate

end
def afterCreate

end
def afterSave

end
end

更新对象顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class User < ApplicationRecord
before_validation :beforeValidation
after_validation :afterValidation
before_save :beforeSave
before_update :beforeUpdate
after_update :afterUpdate
after_save :afterSave



def beforeValidation

end
def afterValidation

end
def beforeSave

end
def beforeUpdate

end
def afterUpdate

end
def afterSave

end
end

销毁对象顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class User < ApplicationRecord
before_destroy :beforeDestroy
after_destroy :afterDestroy



def beforeDestroy

end
def afterDestroy

end

end
文章目录
  1. 1. 回调
    1. 1.1. 创建对象顺序
    2. 1.2. 更新对象顺序
    3. 1.3. 销毁对象顺序
|