Active Record Scope:

default_scope:   默认的检索方式

#Use unscoped to break out of the default 

class Hobby < ActiveRecord::Base
has_and_belongs_to_many :people default_scope { order :name }
end Hobby.pluck :name
# => ["Music","Programming"] Hobby.unscoped.pluck :name
# => ["Programming", "Music"] #named scopes
#scope :name, lambda scope :ordered_by_age, -> {order age: :desc}
scope :starts_with, -> (starting_string) {where("first_name LIKE ?", "#{starting_string}%")} Person.ordered_by_age.pluck :age
# => [,,,,] Person.ordered_by_age.starts_with("Jo").pluck :age, :first_name
# => [[,"Josh"],[,"John"],[,"John"]] Person.ordered_by_age.limit().starts_with("Jo").pluck :age, :first_name
# => [[,"Josh"],[,"John"]]

Scope 总是返回ActiveRecord::Relation

Validations:

数据总是要经过Validations才能存在数据库之中

#:presence and :uniqueness

presence: true
# Make sure the field contains some data uniqueness: true
# 确保只有一个data class Job < ActiveRecord::Base
belongs_to :person
has_one :salary_range validates :title, :company, presence: true
end #Other Common Validators :umericality
# validates numeric input :length
# validates value is a certain length :format
# validates value complies with some regular expression format :inclusion
# validates value is inside specified range :exclusion
# validates value is out of the specified range

Writing your own validation:

class SalaryRange < ActiveRecord::Base
belongs_to :job validate :min_is_less_than_max def min_is_less_than_max
if min_salary > max_salary
errors.add(:min_salary, "cannot be greater than maximum salary!")
end
end
end

N+1 Queries Issue and DB Transactions:

#solve for n+ queries 

Person.includes(:personal_info).all.each { |p| puts p.personal_info.weight }

RoR - More Active Record的更多相关文章

  1. RoR - Introduction to Active Record

    Active Record: ORM ( Object-relational Mapping)Bridges the gap between relational databases , which ...

  2. Yii的学习(5)--Active Record的关联

    官网原文:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.arr 官网中后半段为英文,而且中文的内容比英文少一些,先放到这里,之后有时 ...

  3. Yii的学习(4)--Active Record

    摘自Yii官网:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.ar 在官网原文的基础上添加了CDbCriteria的详细用法. 虽然 ...

  4. Active Record 数据库模式-增删改查操作

    选择数据 下面的函数帮助你构建 SQL SELECT语句. 备注:如果你正在使用 PHP5,你可以在复杂情况下使用链式语法.本页面底部有具体描述. $this->db->get(); 运行 ...

  5. DAL、DAO、ORM、Active Record辨析

    转自:http://blog.csdn.net/suiye/article/details/7824943 模型 Model 模型是MVC中的概念,指的是读取数据和改变数据的操作(业务逻辑).一开始我 ...

  6. Active Record: 資料庫遷移(Migration) (转)

    Active Record: 資料庫遷移(Migration) Programming today is a race between software engineers striving to b ...

  7. Android开源库--ActiveAndroid(active record模式的ORM数据库框架)

    Github地址:https://github.com/pardom/ActiveAndroid 前言 我一般在Android开发中,几乎用不到SQLlite,因为一些小数据就直接使用Preferen ...

  8. Active Record快速入门指南

    一.概述 Active Record(中文名:活动记录)是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录.关系型数据库往往通过外键来表述实体关系,A ...

  9. Yii Active Record 查询结果转化成数组

    使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...

随机推荐

  1. zabbix3.2监控mongodb

    模板地址:https://share.zabbix.com/databases/mongodb/mongodb-for-zabbix-3-2 .安装jq依赖 # yum install jq -y . ...

  2. Sql Server 字符串操作总结

    SQL Server 支持两种字符数据类型---常规和Unicode:常规类型包括char 和varchar:unicode包括nchar 和nvarchar.常规的每个字符占用一个字节存储,而uni ...

  3. python中关于变量名失效的案例

    案例一:传参动态导入模块. selectModule = input("please input your module name") app_name = input(" ...

  4. echart 饼状图自定义样式

    echarts.init(document.getElementById('WaterCategoryStatistics')).setOption({ legend: { orient:'verti ...

  5. Go Socket实现简单的HttpServer

    在上篇博客中写到如何用Python实现一个类似tomcat的简单服务器,接下来用go语言去实现 1. Go本身自己封装实现了非常简单的httpServer package main import ( ...

  6. 期货大赛项目|五,表格插件datatatables在MVC中的应用

    系统中都需要表格,我见过最好的表格就是Datatables了,但中文文档有限,英文能力有限,就写一些简单用法 上图看效果先 要了分页和排序 基本用法 引入js和css bundles.Add(new ...

  7. requests之headers 'Content-Type': 'text/html'误判encoding为'ISO-8859-1'导致中文text解码错误

    0. requests不设置UA 访问baidu 得到 r.headers['Content-Type'] 是text/html  使用chrome UA: Content-Type:text/htm ...

  8. Spring以及tomcat中的Listener

    tomcat容器的listener: ServletContextListener HttpSessionListener ServletRequestListener Spring的listener ...

  9. WEB测试重点

    1.功能测试:所实现的功能是否和需求一致:2.界面测试:界面是否美观,风格是否一致,文字内容是否正确:3.链接测试:打开链接速度是否合理:是否链接到正确的页面:是否有空白页面:4.性能测试:系统能支持 ...

  10. RecyclerView滑动到指定位置,并置顶

    一般我们用 mRecycleview.smoothScrollToPosition(0)滑动到顶部,具有滚动效果,但是如果我们想滚动到任意指定位置,那么smoothScrollToPosition() ...