模型(model)

作者: 跳格式工厂 | 来源:发表于2018-05-24 16:51 被阅读0次

    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、指定运行环境

    rails console test/development/production  #默认development

    2、新建

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

    3、保存

    demo.save

    2、3两步骤直接 用create

    获取最后一个数据

    last = ModelTest.last

    基本数据检索

    1、主键(key)搜索:Class.find(keys) #返回主键所对应的数据(一对一)

    2、指定键搜索:Class.find_by(key,value[,..]) #返回找到的第一个,可以指定多个键来追加限制 如果搜索不存在需要创建则使用 Class.find_or_create_by(key,value[,..])

    复杂条件下的数据检索

    1、设置基本条件

    Class.where(exp) #exp 条件式哈希表形式

    使用joins/includes等关联搜索的参数写法如下:

    where(关联表格名:{关联处的搜索条件})

    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(...)  # 参数和where一样

    4、or

    Class.where(...).or(Class.where(...))

    ModelTest.where('ap  <= ?', 1000).or(ModelTest.where('def > :def', :def => 4000))

    5、 排序

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

    参数 sort:排序式,默认:asc,可以省略 。:desc 降序,:asc 升序。

    6、重排

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

    写法和order一样,作用是覆盖前面的order,如果只是想清空前面的order,指定nil

    7、指定读取的列

    Class.where(...).select(cols) # 默认获取所有的列, 用这个方法可以指定具体要获得的列 

    8、去除重复

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

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

    # flag = true 去除重复 flag =false 保留重复 默认为 true

    8、获取特定范围

    limit/offset ,和order一起用才有现实意义

     limit(rows) #rows 最多获取的行数

     offset(off) #off 开始获取的位置(从0开始,我们熟悉的偏移量)

    例如:

    ModelTest.where('hp >= ? AND mp >= ? AND ad >= ?', 1000, 200, 1500).offset(1).limit(1)

    9、获取开头/结尾数据

     Class.first

     Class.last

    (也可以用limit(0))

    #不能惰性读取,必须放在方法链最后

    10、回调函数

    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   回调后

    11、 Data/Time相关的有用的方法

    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 #也可以用单数

    n.xxx.from_now #Numeric,n个年/月/日/时/分/秒以后

    years, months, days, hours, minutes, seconds,也可以用单数

    相关文章

      网友评论

        本文标题:模型(model)

        本文链接:https://www.haomeiwen.com/subject/zekqjftx.html