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, ...
Basics of Research Paper Writing and Publishing.pdf
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 ...
BASiCS:BASiCS:单细胞测序数据的贝叶斯分析。 这是不稳定的实验版本。 请参阅http://bioconductor.orgpackagesBASiCS以获得正式发行版本
数字滤波器:基础知识与设计方法,第二版,非扫描,带书签。设计滤波器很好的书籍
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 ...
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, ...
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 ...
Step 5: Disambiguating the Term Step 6: Understanding the Nature of Ownership Step 7: Spending Money Twice Stage III: How the Blockchain Works Step 8: Planning the Blockchain Step 9: Documenting ...
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 ...
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 ...
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...
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 ...
Chapter 5: Virtualization and Cloud Basics Chapter 6: Information Security Basics Chapter 7: Penetration Testing Chapter 8: Information Gathering Chapter 9: Hacking Basics Chapter 10: Web Application ...
黑客 linux 基础命令行 文件系统 网络 shell 脚本 包管理 日志记录 Linux内核 驱动程序
languages and how it can answer most of the data processing tasks that we Introduction xv highlight in this book. We conclude with some examples of programming by “patterns”. – Part 2: This ...