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 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...
随机推荐
- Lua中的一些库(2)
[前言] 在<Lua中的一些库(1)>这篇文章中,总结了一部分Lua中的库函数,一篇文章肯定是总结不完的,所以,就来一个<Lua中的一些库(2)>.希望大家能忍住.来吧. 操作 ...
- python学习第24天
内置方法 常用 __new__ __del__ __call__ 不常用 __str__ __repr__ __enter__ __exit__
- 【原创】大叔问题定位分享(22)hive同时执行多个insert overwrite table只有1个可以执行
hive 2.1 一 问题 最近有一个场景,要向一个表的多个分区写数据,为了缩短执行时间,采用并发的方式,多个sql同时执行,分别写不同的分区,同时开启动态分区: set hive.exec.dyna ...
- vue路由守卫(全局守卫)
router.beforeEach((to,from,next)=>{}) 回调函数中的参数, to:进入到哪个路由去, from:从哪个路由离开, next:函数,决定是否展示你要看到的路由页 ...
- spark MLlib collaborativeFilltering学习
package ML.collaborativeFilltering; import org.apache.spark.SparkConf; import org.apache.spark.api.j ...
- Maven中pom.xml文件的配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- skywalking6.0.0安装配置(windows),以mysql作为储存。
下载skywalking6.0.0http://skywalking.apache.org/downloads/ 下载jdk8https://www.oracle.com/technetwork/ja ...
- SA:T1编写主函数法和T2Matlab自带的SA工具箱GUI法,两种方法实现对二元函数优化求解——Jason niu
%SA:T1法利用Matlab编写主函数实现对定义域[-5,5]上的二元函数求最优解—Jason niu [x,y] = meshgrid(-5:0.1:5,-5:0.1:5); z = x.^2 + ...
- 【mybatis】-- springboot整合mybatis
1.添加依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>m ...
- Django——RESTful架构
一.REST简述 来自维基百科的解释: 表现层状态转换(REST,英文:Representational State Transfer)是Roy Thomas Fielding博士于2000年在他的博 ...