model的生成
rails generate model name field:type [...] [options] #TODO:options 類型首字母不大寫
例子:
rails g model questionnaire question:string
veryAgree:boolean agree:boolean
disagree:boolean veryDisagree:boolean
遷移文件的生成
和模型一起生成
rails generate model name field:type [...] [options]
單獨(dú)生成
rails generate migration name [field:type ...] [options]
例子:
rails generate migration AddBirthToAuthors birth: date
自帶屬性
1躏吊、id #主鍵
2短蜕、create_at #記錄(record)的生成時(shí)間
3、update_at #記錄(record)的更新時(shí)間
rails 命令行
1片橡、啟動(dòng)
? ? rails c (console)
? ? 1.1栈顷、指定運(yùn)行環(huán)境
rails console test/development/production? #默認(rèn)development
2熬荆、新建
demo = modelTest.new(math:50,chinese:89,english:99)
3歹河、保存
demo.save
2攒岛、3兩步驟直接 用create
獲取最后一個(gè)數(shù)據(jù)
last = ModelTest.last
基本數(shù)據(jù)檢索
1论寨、主鍵(key)搜索:Class.find(keys) #返回主鍵所對(duì)應(yīng)的數(shù)據(jù)(一對(duì)一)
2星立、指定鍵搜索:Class.find_by(key,value[,..]) #返回找到的第一個(gè),可以指定多個(gè)鍵來(lái)追加限制 如果搜索不存在需要?jiǎng)?chuàng)建則使用 Class.find_or_create_by(key,value[,..])
復(fù)雜條件下的數(shù)據(jù)檢索
1葬凳、設(shè)置基本條件
Class.where(exp) #exp 條件式哈希表形式
使用joins/includes等關(guān)聯(lián)搜索的參數(shù)寫法如下:
where(關(guān)聯(lián)表格名:{關(guān)聯(lián)處的搜索條件})
cars = cars.includes(:equipment_spec).where(equipment_specs: { power_window: true })
2绰垂、 用占位符生成條件式
Class.where(exp [, value, ...]) #占位符: ?或者符號(hào)(symbol) :sample
注意: 一定要用占位符,別把輸入的字符直接展開進(jìn)去火焰。SQL可能會(huì)爆炸的233
3劲装、where的否定
Class.where.not(...)? # 參數(shù)和where一樣
4、or
Class.where(...).or(Class.where(...))
ModelTest.where('ap? <= ?', 1000).or(ModelTest.where('def > :def', :def => 4000))
5、 排序
Class.where(...).order(sort)
參數(shù) sort:排序式占业,默認(rèn):asc绒怨,可以省略 。:desc 降序谦疾,:asc 升序南蹂。
6、重排
Class.where(...).order(sort).reorder(sort)
寫法和order一樣念恍,作用是覆蓋前面的order六剥,如果只是想清空前面的order,指定nil
7、指定讀取的列
Class.where(...).select(cols) #?默認(rèn)獲取所有的列, 用這個(gè)方法可以指定具體要獲得的列?
8樊诺、去除重復(fù)
Class.where(...).distinct(flag)
?Class.select(...).distinct(flag)
# flag = true 去除重復(fù) flag =false 保留重復(fù) 默認(rèn)為 true
8仗考、獲取特定范圍
limit/offset 音同,和order一起用才有現(xiàn)實(shí)意義
?limit(rows) #rows 最多獲取的行數(shù)
?offset(off) #off 開始獲取的位置(從0開始词爬,我們熟悉的偏移量)
例如:
ModelTest.where('hp >= ? AND mp >= ? AND ad >= ?', 1000, 200, 1500).offset(1).limit(1)
9、獲取開頭/結(jié)尾數(shù)據(jù)
?Class.first
?Class.last
(也可以用limit(0))
#不能惰性讀取权均,必須放在方法鏈最后
10顿膨、回調(diào)函數(shù)
before_validation???驗(yàn)證處理前
?after_validation???驗(yàn)證處理后
?before_save???保存前
?around_save???保存前后
?before_create?before_update?before_destroy?新建/更新/刪除前?
?around_create?around_update?around_destroy?新建/更新/刪除前后
?after_create?after_update?after_destroy??新建/更新/刪除后
?after_save???保存后
?after_commit???commit后?
?after_rollback???回調(diào)后
11叽赊、?Data/Time相關(guān)的有用的方法
yesterday?昨天
?tomorrow?明天
?prev_xxxx?前年/月/周(year恋沃,month,week)
?next_xxxx?下年/月/周(year必指,month囊咏,week)
?beginning_of_xxxx?年/季/月/周的開始一天(year, quarter, month, day)
?end_of_xxxx?年/季/月/周的最后一條(year, quarter, month, day)
n.xxx.ago #Numeric,n個(gè)年/月/日/時(shí)/分/秒以前
years, months, days, hours, minutes, seconds #也可以用單數(shù)
n.xxx.from_now #Numeric塔橡,n個(gè)年/月/日/時(shí)/分/秒以后
years, months, days, hours, minutes, seconds梅割,也可以用單數(shù)