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

删除所有数据

最新文章

  1. 我的iOS开发系列博文
  2. unity3D 常用快捷键
  3. 高性能JavaScript 重排与重绘
  4. [POJ1765]November Rain
  5. sender是什么意思C#
  6. 现代浏览器原生js获取id号方法
  7. [转载]Div和Table的区别
  8. 转:几十种编程语言的快速入门教程- learnxinyminutes.com
  9. 使用Userlock监控用户访问 增强学校网络安全
  10. poj3233之经典矩阵乘法
  11. mybatis操作动态表+动态字段+存储过程
  12. iOS英语—》中国本土化,如调用专辑,摄像头的变化“cancel”,“photos”至“撤消”,“摄像头”
  13. PureMVC(JS版)源码解析
  14. Java NIO (三) 通道(Channel)
  15. monkey日志分析
  16. 未能从程序集“mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.Runtime.CompilerServices.TuppressIldasmAttribute”。已解决
  17. 【转】史上最详细的Composer安装tp5教程
  18. MySQL:缓存算什么东西?!
  19. 014 在Spark中完成PV与UV的计算,重在源代码
  20. 剥开比原看代码08:比原的Dashboard是怎么做出来的?

热门文章

  1. SQL DCL 数据控制语句
  2. MySQL遇到Deadlock found when trying to get lock,解决方案
  3. 关于 Docker Hub 上不能注册 Docker ID 的问题
  4. YARN Resource Management
  5. MYSQL 优化器 源码解析
  6. asp.net mvc 微信支付代码分析(根据沐雪微信平台3.1商城业务来分析)
  7. REST与SOA两种架构的异同
  8. Matlab信号处理基础
  9. JSP通过AJAX获取服务端的时间,在页面上自动更新
  10. 【BZOJ4025】二分图