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. Error: Could not find or load main class Test

    问题描述 Linux 环境下运行 Java 程序时,执行 javac Test.java 生成 Test.class 文件,再执行 java Test 时报错:Error: Could not fin ...

  2. Jmeter性能测试之分布式(五)

    Jmeter是纯Java开发的开源性能测试工具, Java程序是很吃内存的, 有时候一台负载机给服务器的压力是不够的, 需要很多台同时负载, 这个时候就需要用到分布式了. 1. 组网图大概就是这样的 ...

  3. ansible的Filter

    filter的格式:   value..| filter() 在python中就是类的实例化 filter(self,*args,**kwargs) self就是filter中管道符前的value. ...

  4. Visual Studio 2017

    美国西雅图时间 3 月 7 日上午 9 点(北京时间 8 日凌晨 1 点),微软将正式发布 Visual Studio 2017.   下载地址:https://www.visualstudio.co ...

  5. CodeForces 286E Ladies' Shop 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8781889.html 题目传送门 - CodeForces 286E 题意 首先,给你$n$个数(并告诉你$m$ ...

  6. yum和rpm

    rpm简介 这是一个数据库管理工具,可以通过读取数据库,判断软件是否已经安装,如果已经安装可以读取出来所有文件的所在位置等,并可以实现删除这些文件. rpm:RPM is Redhat Package ...

  7. 【Linux】-- 在linux上安装mysql及基本操作

    1.MySQL的安装 1.删除mariadb数据库 yum remove mariadb-libs.x86_64 CentOS7默认安装mariadb数据库,所以要先删除 2.下载mysql源 进入m ...

  8. 第九篇 蓝图 blueprint

    在Flask中的蓝图 blueprint 的作用就是将 功能 与 主服务 分开 比如说,你有一个客户管理系统,最开始的时候,只有一个查看客户列表的功能,后来你又加入了一个添加客户的功能(add_use ...

  9. 为什么很多应用都安装在/usr/local目录下?

    首先,Linux的官方文档FHS对该目录的说明:http://www.linuxbase.org/betaspecs/fhs/fhs/ch04s09.html The /usr/local hiera ...

  10. [LeetCode] Letter Case Permutation 字母大小写全排列

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...