模型(model)

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]

單獨生成

rails generate migration name [field:type ...] [options]

例子:

rails generate migration AddBirthToAuthors birth: date

自帶屬性

1、id #主鍵

2、create_at #記錄(record)的生成時間

3、update_at #記錄(record)的更新時間

rails 命令行

1、啟動

? ? rails c (console)

? ? 1.1、指定運行環(huán)境

rails console test/development/production? #默認development

2、新建

demo = modelTest.new(math:50,chinese:89,english:99)


3、保存

demo.save

2、3兩步驟直接 用create

獲取最后一個數(shù)據(jù)

last = ModelTest.last

基本數(shù)據(jù)檢索

1、主鍵(key)搜索:Class.find(keys) #返回主鍵所對應(yīng)的數(shù)據(jù)(一對一)

2、指定鍵搜索:Class.find_by(key,value[,..]) #返回找到的第一個,可以指定多個鍵來追加限制 如果搜索不存在需要創(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, ...]) #占位符: ?或者符號(symbol) :sample

注意: 一定要用占位符,別把輸入的字符直接展開進去。SQL可能會爆炸的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:排序式,默認:asc,可以省略 。:desc 降序,:asc 升序。

6、重排

Class.where(...).order(sort).reorder(sort)

寫法和order一樣,作用是覆蓋前面的order,如果只是想清空前面的order,指定nil

7、指定讀取的列

Class.where(...).select(cols) #?默認獲取所有的列, 用這個方法可以指定具體要獲得的列?

8、去除重復(fù)

Class.where(...).distinct(flag)

?Class.select(...).distinct(flag)

# flag = true 去除重復(fù) flag =false 保留重復(fù) 默認為 true

8、獲取特定范圍

limit/offset ,和order一起用才有現(xiàn)實意義

?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???驗證處理前

?after_validation???驗證處理后

?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個年/月/日/時/分/秒以前

years, months, days, hours, minutes, seconds #也可以用單數(shù)

n.xxx.from_now #Numeric,n個年/月/日/時/分/秒以后

years, months, days, hours, minutes, seconds,也可以用單數(shù)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 我的青春就是一條恬靜又平穩(wěn)湖,落葉掉落在湖面上會泛起很淺的漣漪,就一直在悄無聲息的存在在那里。永遠不會像...
    梅遜閱讀 164評論 0 1
  • 又來到了 這座荒蕪的城池 曾經(jīng)的輝煌 如今已是另一番景象 絞繩似的藤蔓 孤獨的纏在斑駁的老墻上 毒蕈在沼泥中肆意瘋...
    西涼乃閱讀 332評論 0 3

友情鏈接更多精彩內(nèi)容