首先登上舞臺(tái)的是,find方法:
find方法
find 記住一點(diǎn)就行,如果找不到記錄,會(huì)報(bào)錯(cuò)(其它一般會(huì)加!才會(huì))
where 方法
主要說兩點(diǎn):
1.條件占位符
User.where('name = :name and age > :begin_age',name: 'dm',begin_age: 24)
2.關(guān)聯(lián)查找
author = Author.first
Book.where(author: author)
enum 宏將數(shù)值字段映射成 描述
這就是一個(gè)映射關(guān)系,不用我們自己去寫hash
class Order < ApplicationRecord
enum :status => [:place_on_order,:shipment]
# 0 - place_on_order
# 1 - shipment
end
判斷存在exists? /any?/some?
1、
exists?可接參數(shù)
User.exists?(:name => 'df')
2、some?超過一個(gè) 返回true
3、any?只要有一個(gè) 返回 true
find_or_xxx_by找不到就創(chuàng)建/初始化
1、
find_or_create_by找不到就創(chuàng)
2、find_or_initialize_by初始化
PS: 加字段 用create_with