version: rails 4
CRUD是四个动作的缩写, 我们用来操作数据: 创建(Create), 查询(Read), 更新(Update)和删除(Delete). Active Record自动创建方法, 允许一个应用来查询和操作已被保存在数据表中的数据.
5.1 Create 创建
Active Record 对象能够从一个哈希队列, 一个代码库中被创建, 或者在创建之后手工地设置他们的特性. new方法会返回一个新的对象, 而当create 返回的对象会被保存到数据库中.
例如, 有一个模型 User, 它的特性是name 和 occupation, 方法create 的调用会被创建和保存一条新的记录到数据库中.
user = User.create(name: "David" , occupation: "Code Artist" )
|
使用方法new, 一个对象能够被初始化, 而没有被保存:
user = User. new
user.name = "David"
user.occupation = "Code Artist"
|
对于user.save的调用将会提交记录到数据库中.
最后, 如果有一个代码块, 主要用来初始化, create和new都会创建一个新的对象.
user = User. new do |u|
u.name = "David"
u.occupation = "Code Artist"
end |
5.2 Read 查询
Active Record 提供了许多的API用来从数据库中读取数据. 下文是一些不同数据读取方法的例子, 都是由 Active Record 提供的.
# return a collection with all users users = User.all |
# return the first user user = User.first |
# return the first user named David david = User.find_by(name: 'David' )
|
# find all users named David who are Code Artists and sort by created_at in reverse chronological order users = User.where(name: 'David' , occupation: 'Code Artist' ).order( 'created_at DESC' )
|
你可以学到更到的查询一个Active Record 模型, 参考 Active Record Query Interface
5.3 Update 更新
一旦一个 Active Record 对象被重新检索到, 它的属性能够被修改, 以及它能够被保存到数据库中.
user = User.find_by(name: 'David' )
user.name = 'Dave'
user.save |
用一个哈希映射特性的名称对应于期望的值有一个快捷的实现, 类似于下文:
user = User.find_by(name: 'David' )
user.update(name: 'Dave' )
|
这个相当的有用, 当需要马上更新一些特性的时候. 也就是说, 如果你想要在一个代码块中更新多条记录,你可能会发现类中的update_all 方法非常有用:
User.update_all "max_login_attempts = 3, must_change_password = 'true'"
|
5.4 Delete 删除
同样的,被接收到的一个Active Record 对象能够被销毁,也就是从数据库中删除它。
user = User.find_by(name: 'David' )
user.destroy |
相关推荐
CHAPTER 5: PERFORMING CRUD OPERATIONS 97 CHAPTER 6: QUERYING NOSQL STORES 117 CHAPTER 7: MODIFYING DATA STORES AND MANAGING EVOLUTION 137 CHAPTER 8: INDEXING AND ORDERING DATA SETS 149 CHAPTER 9: ...
If you are a programmer or a data analyst familiar with the Python programming language and want to perform analyses of your social data to acquire valuable business insights, this book is for you....
The book has been divided into four sections: Algorithm Basics, Data Structures, Design Techniques and Advanced Topics. The first section explains the importance of algorithms, growth of functions, ...
Research Basics: Design to Data Analysis in Six Steps offers a fresh and creative approach to the research process based on author James V. Spickard’s decades of teaching experience. Using an ...
同时,随着数字信号处理技术的不断进步,数字滤波器的应用领域也在不断扩展,从传统的音频和视频处理到现代的无线通信、图像处理、数据压缩和语音识别等,数字滤波器都扮演着重要的角色。 综合来看,《数字滤波器:...
Chapter 5: Arrays Chapter 6: Object-Oriented PHP Chapter 7: Advanced OOP Features Chapter 8: Error and Exception Handling Chapter 9: Strings and Regular Expressions Chapter 10: Working with the File ...
Start with the basics of Core Data and learn how to use it to develop your application. Then delve deep into the API details. Explore how to get Core Data integrated into your application properly, ...
《 Beginning jQuery:From the Basics of jQuery to Writing your Own Plug-ins》是Jack Franklin和Russ Ferguson合著的一本关于jQuery的入门教程。这本书旨在帮助读者从零基础开始,逐步掌握jQuery的核心概念,并...
This book gives readers the opportunity to learn the basics at the high level (HDLs), at the low level (electrical circuits), and throughout the “vast middle” (gates, flip-flops, and higher-level ...
Evan Hahn is an active member of the Node and Express community and contributes to many open source JavaScript projects. Table of Contents PART 1 INTRO What is Express? The basics of Node.js ...
Data Analytics and Linux Operating System. Beginners Guide to Learn Data Analytics, Predictive Analytics and Data Science with Linux Operating System (Hacking Freedom Data Driven) This is a 2 book ...
Title: Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython, 2nd Edition Author: Wes McKinney Length: 550 pages Edition: 2 Language: English Publisher: O'Reilly Media Publication ...
**BASiCS**,全称为Bayesian Analysis of Single Cell Sequencing Data,是一个专门用于单细胞测序数据的贝叶斯分析工具。该工具旨在处理单细胞RNA测序(scRNA-seq)数据,这是一种在单个细胞层面上研究基因表达的高...
Ideal for developers familiar with Java, Android basics, and the Java SE API, this book features recipes contributed by more than three dozen Android developers. Each recipe provides a clear solution ...
1. 准备阶段:确定研究主题,进行文献回顾,设计实验,收集数据。 2. 撰写阶段:基于研究结果,按照论文结构撰写初稿。 3. 修改阶段:自我审阅,修正语法错误,提升论文逻辑性和清晰度。 4. 投稿阶段:选择合适的...
### Analyzing Neural Time Series Data: Key Insights and Concepts #### Introduction The book "Analyzing Neural Time Series Data: Theory and Practice" by Mike X Cohen is a comprehensive guide designed...
Title: Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython, 2nd Edition Author: Wes McKinney Length: 550 pages Edition: 2 Language: English Publisher: O'Reilly Media Publication D...
Erlang and OTP in Action Martin Logan, Eric Merritt, and Richard Carlsson MEAP Began: August 2008 Softbound print: May 2010 (est.) | 500 pages ISBN: 1933988789 Part One: Getting Past Pure Erlang; ...
Get to grips with the basics of Elasticsearch concepts and its APIs, and use them to create efficient applications Create large-scale Elasticsearch clusters and perform analytics using aggregation A ...