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 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...
随机推荐
- C++设计模式——装饰模式
前言 在实际开发时,你有没有碰到过这种问题:开发一个类,封装了一个对象的核心操作,而这些操作就是客户使用该类时都会去调用的操作:而有一些非核心的操作,可能会使用,也可能不会使用:现在该怎么办呢? 将这 ...
- CIA402状态转换图
CIA402状态转换如下图所示: 要想改变参数并使其生效,需要先将状态转换到ready,然后修改要配置的参数,再使其运行(operation enabled). 要发送的报文顺序基本如下: 1) ...
- ApowerMirror投屏(手机投屏电脑、电脑投屏到手机)
使用步骤 1. 亲测 使用Apowersoft ApowerMirror v1.4.2.zip版本 2.Apowersoft ApowerMirror v1.4.2.zip 解压安装 ...
- 使用Java方式连接HDFS
IDEA中新建Maven工程,添加POM依赖, 在IDE的提示中, 点击 Import Changes 等待自动下载完成相关的依赖包. <?xml version="1.0" ...
- (一)Knockout 计算属性
1 Computed 首先,创建一个view model如下: <body> <p>The fullname is: <span data-bind="text ...
- python 10大算法之一 LinearRegression 笔记
简单的线性回归预测房价 #!/usr/bin/env python # encoding: utf-8 """ @version: @author: --*--. @fi ...
- Linux终端复用神器-Tmux使用梳理
Tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权.使用它最直观的好处就是,通过一个终端登录远程主机并运行tmux后,在其中可以开启多个控制台而无需再“ ...
- Mqtt使用教程,简介
1,简介 MQTT协议(Message Queuing Telemetry Transport),翻译过来就是遥信消息队列传输,是IBM公司于1999年提出的,现在最新版本是3.1.1.MQTT是一个 ...
- 在使用EF时,想要比较字符串类型的日期时
原文地址(https://blog.csdn.net/yangxinyue315/article/details/44960895) 在使用EF时,想要比较字符串类型的日期时 在使用EF时,想要比较字 ...
- php curl 跨域情趣
function curl_post($url='',$postdata='',$options=array()){ $ch=curl_init($url); curl_setopt($ch,CURL ...