Active Record:

ORM ( Object-relational Mapping)
Bridges the gap between relational databases ,

which are designed around mathematical Set Theory and Object

Oriented programming languages that deal with objects and their behavior

* Greatly simplifies writing code for accessing the database

*In rails, the model(usually) uses some ORM framwork.

Active Record is also the name of Rails' default ORM

1. Actuve Record has to know how to find your databse(When Rails is loaded,  this info is read from config/database.yml file)

2.(Convention) There is a table with a plural name that corresponds to ActiveRecord::Base subclass with singular name

3.(Converntion) Expects the table to have a primary key named id

Model and Migration:

rails g model person first_name last_name    --- generate model

reload! --- After rake db:migrate

Active Record conventions:

*Class name is singular

*DB table name is plural

*Need to have an id primary key

Active Record CRUD:

Create:

有3种方法 在数据库中Create a record

1. 用空的构造器和(ghost) attributes 去设置数据然后call save 保存

2.传hash of attribute到构造器中然后call save 保存

3.用create方法,用hash去创建一个对象然后把它保存到数据库中(只需1步)

#
p1=Person.new; p1.first_name = "Joe"; p1.last_name ="Smith"
p1.save #2
p2 = Person.new(first_name: "John", last_name:"Doe");
p2.save #
p3=Person.create(first_name:"Jane",last_name:"Doe")

Retrieve/Read :

1. find(id) or find(id1, id2)

如果没找到 抛出 RecordNotFound exception

2. first, last, take, all

返回你期望的结果或者nil 如果不存在

3. order(:column) or order(column: :desc)

排序返回的结果,升序或者降序

4.pluck

*allows to narrow down thich fields are coming back

*Need to call at the end!

Person.take 2 随机从数据库中抽出2个对象

Person.all.map { |person| person.first_name } map返回所有的first_name

Person.pluck(:first_name)  用法同上,不过是在database level

where(hash)

Person.where(last_name: "Doe") 返回名字里有Doe

Person.where(last_name: "Doe").first 第一个返回名字里有Doe的

Person.where(last_name: "Doe").pluck(:firtst_name)   pluck is not active record

find_by(conditions_hash)

Person.find_by(last_name: "Doe")  只给一个record

Person.find_by(last_name: "xxx")    #=>  nil

Person.find_by!(last_name: "xxx")   ActiveRecord::RecordNotFound: Couldn't find Person

limit: 限制返回多少条records

offset(n):跳过前面n条records

Person.count   =>3

Person.all.map {|person| "#{person.first_name} #{person.last_name" }
=> ["Joe smithson", "John Doe", "Jane smithie"] Person.offset(1).limit(1).map {|person| "#{person.first_name} #{person.last_name "}
=>["John Doe"]

Update:

1. 取出record,对它进行编辑然后save

2.检索你想要编辑的record,call update method 传新的变量的hash进去

Delete:

destory(id) 或者 destory

从数据库里移除特定的instance

先实例化一个对象,然后在移除之前执行回调

delete(id)

从数据库中删除特定行

delete_all

删除所有数据

RoR - Introduction to Active Record的更多相关文章

  1. RoR - More Active Record

    Active Record Scope: default_scope:   默认的检索方式 #Use unscoped to break out of the default class Hobby ...

  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. MVC Json方法里的一个坑

    MVC Controller类下面有这样一个方法 // // Summary: // Creates a System.Web.Mvc.JsonResult object that serialize ...

  2. 【C++】C++中的引用与指针

    想必大家对C++中的指针都有所了解,但是什么是引用呢?C++11标准引入了“引用”的新功能. 引用 引用(reference):给对象起了另外一个名字,引用类型引用(refers to)另外一种类型, ...

  3. Git回滚代码到某个commit

    回退命令: $ git reset --hard HEAD^ 回退到上个版本$ git reset --hard HEAD~3 回退到前3次提交之前,以此类推,回退到n次提交之前 $ git rese ...

  4. asp.net中 使用参数化mysqlparameter 保存数据时,总保存成一个汉字的解决方案。

    var param = new MySqlParameter("@" + columName, property.Value); param.DbType = DbType.Str ...

  5. Hadoop相关基础知识

    因为个人对这块的知识也不熟悉,所以大多内容来源于网络. 1.      Hadoop项目框架 2.      Hadoop Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以 ...

  6. mac:Go安装和配置+GoLand安装和使用之完整教程

    前言 作为一个go语言程序员,觉得自己有义务为go新手开一条更简单便捷的上手之路.纵使网上教程很多,但总不尽人意.go的入门门槛还是非常低的,无论是安装还是使用. go安装 go 语言支持以下系统:  ...

  7. 项目中 2个或者多个EF模型 表名称相同会导致生成的实体类 覆盖的解决方法

    场景:  2个数据库, 一个新,一个旧,  把旧的 数据库中的数据,导入到新的数据库中,  使用到了2个 EF实体模型, 新数据库 和 旧数据库中的表,有的名称是相同的 (但是结构是不同的) 旧的数据 ...

  8. [原创] 如何PCB通流能力计算

    一.计算方法如下: 先计算Track的截面积,大部分PCB的铜箔厚度为35um(不确定的话可以问PCB厂家)它乘上线宽就是截面积,注意换算成平方毫米. 有一个电流密度经验值,为15~25安培/平方毫米 ...

  9. 深度讲解 .net session 过期机制

    [参考]net session过期 原理及解决办法 [参考]深入理解session过期机制

  10. Apple Watch S3 解锁 MacBook Pro 2015版失败的解决办法

    我的MacBook Pro MF839由于只有128G的内存,所以就只能藏在我的抽屉底下,偶尔想体验一下xcode的时候再拿回来用下,想想都浪费 也不是不想换SSD,只是看了一下,价格太贵了,256G ...