RoR - More Active Record
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的更多相关文章
- RoR - Introduction to Active Record
Active Record: ORM ( Object-relational Mapping)Bridges the gap between relational databases , which ...
- Yii的学习(5)--Active Record的关联
官网原文:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.arr 官网中后半段为英文,而且中文的内容比英文少一些,先放到这里,之后有时 ...
- Yii的学习(4)--Active Record
摘自Yii官网:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.ar 在官网原文的基础上添加了CDbCriteria的详细用法. 虽然 ...
- Active Record 数据库模式-增删改查操作
选择数据 下面的函数帮助你构建 SQL SELECT语句. 备注:如果你正在使用 PHP5,你可以在复杂情况下使用链式语法.本页面底部有具体描述. $this->db->get(); 运行 ...
- DAL、DAO、ORM、Active Record辨析
转自:http://blog.csdn.net/suiye/article/details/7824943 模型 Model 模型是MVC中的概念,指的是读取数据和改变数据的操作(业务逻辑).一开始我 ...
- Active Record: 資料庫遷移(Migration) (转)
Active Record: 資料庫遷移(Migration) Programming today is a race between software engineers striving to b ...
- Android开源库--ActiveAndroid(active record模式的ORM数据库框架)
Github地址:https://github.com/pardom/ActiveAndroid 前言 我一般在Android开发中,几乎用不到SQLlite,因为一些小数据就直接使用Preferen ...
- Active Record快速入门指南
一.概述 Active Record(中文名:活动记录)是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录.关系型数据库往往通过外键来表述实体关系,A ...
- Yii Active Record 查询结果转化成数组
使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...
随机推荐
- 浅析nodejs的buffer类(转)
最近翻阅了node v0.10.4的buffer类的源代码,收获不少,也很久没有在cnode上发表文章了,想把一些收获分享给大家,有什么错误的地方希望大牛们指正啊. 前阵子有位rrestjs框架的使用 ...
- JAVA 数组元素的反转
package Code411;/*数组元素的反转本来[1,2,3,4]反转后[4,3,2,1]1.对称位置的元素交换2.对称位子需要两个索引3.int temp =a:a=b;b=temp;4.什么 ...
- 解决Linux(Loaded plugins: fastestmirror Please use /usr/bin/yum --help)
大概意思是fastestmirror不能使用,fastestmirror是yum的一个加速插件 处理办法就是禁用这个插件 方法两种 第一种 vi /etc/yum/pluginconf.d/faste ...
- 做GUI的随笔
用的SDL库 官方网站是:https://littlevgl.com/ 改网站需要FQ 字库制作网站: https://debugdump.com/t_771.html
- 单元测试如何覆盖internal的方法
在类的设计中经常会有类或者方法要设置成private或者internal等方式,在使用中这么做无可厚非,但是对单元测试的影响也颇大 对于private方法,那只有做一个副本然后改成internal或p ...
- CodeForces 528D Fuzzy Search 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8782849.html 题目传送门 - CodeForces 528D 题意 给你两个串$A,B(|A|\geq| ...
- 在js中网页面写入数据时需要注意的几点
网页代码 <tbody id="t_gun"> <s:iterator value="gunList" status="st&quo ...
- springboot的jar包
公用jar包放在api层.有些却包的不用增加pom文件.将已存在的jar包加入即可
- Codeforces 811C Vladik and Memorable Trip (区间异或最大值) (线性DP)
<题目链接> 题目大意: 给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都只能出现在这个区间. 每个区间的价值为该区间不同的数的异或值之和,现在问你这n个数最大的价值是 ...
- 操作mysql(import pymysql模块)
pymysql模块 import pymysql #1.连上数据库.账号.密码.ip.端口号.数据库 #2.建立游标 #3.执行sql #4.获取结果 #5.关闭游标 #6.连接关闭 #charest ...