RoR - Introduction to Active Record
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的更多相关文章
- RoR - More Active Record
Active Record Scope: default_scope: 默认的检索方式 #Use unscoped to break out of the default class Hobby ...
- 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 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...
随机推荐
- SNF快速开发平台2019-APP移动端实际应用效果
废话不多说,直接上效果图: 这是我们移动端框架基本程序+组件库+标准业务程序,当然了还需要配合上我们的代码生成器则更如虎添翼.https://www.cnblogs.com/spring_wang/p ...
- BizTalk Schedule Adapter的使用
由于BizTalk作为一个消息中间件是无状态的,一般不能主动去触发消息.因此在有一些特定的场景,比如每隔X分钟/小时/天去轮询或获取数据时就会特别不方便.不过可以通过Codeplex上的开源项目:Bi ...
- adb获得安卓系统版本及截屏
[时间:2017-09] [状态:Open] [关键词:adb, android,系统版本,截屏,screencap] 本文主要是我遇到的android命令行用法的一个简单总结 系统版本 获取系统版本 ...
- 为什么要使用NoSQL
转载自:http://www.infoq.com/cn/news/2011/01/nosql-why [编者按]NoSQL在2010年风生水起,大大小小的Web站点在追求高性能高可靠性方面,不由自主都 ...
- OpenCV + python 实现人脸检测(基于照片和视频进行检测)
OpenCV + python 实现人脸检测(基于照片和视频进行检测) Haar-like 通俗的来讲,就是作为人脸特征即可. Haar特征值反映了图像的灰度变化情况.例如:脸部的一些特征能由矩形特征 ...
- vscode打造最佳的markdown编辑器
参考:https://www.jianshu.com/p/18876655b452 在macos下也设置成功:
- python 遍历hadoop, 跟指定列表对比 包含列表中值的取出。
import sys import tstree fname = 'high_freq_site.list' tree = tstree.TernarySearchTrie() tree.loadDa ...
- php -- 类对象调用静态方法
以前一直以为 静态方法的调用:类名::静态方法 非静态方法的调用:类对象->非静态方法 最近研究一个类,发现一个比较奇怪的问题,用“类对象->静态方法”这种方式居然成功的调用了静态方法.很 ...
- windows安装mongodb服务简洁版教程
根据网上安装教程,简单总结如下: 1.去mongodb官网下载电脑系统对应版本的软件,比如我的是windows 64位的,就选择64位的,可能下载下来之后文件夹上面显示的是win32,这个不用理会: ...
- 转 .NET4.5之初识async与await
来自:http://www.cnblogs.com/lekko/archive/2013/03/05/2944282.html 本人是从.NET4.0刚出的时候接触的.NET环境,所以学的东西就是4. ...