《Rails-Guides》Reading notes one:Active Record Basics

Preface

????????These are the new guides for Rails 5.1 based on?v5.1.6.?These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together.

What is Active Record?

????????Active Record is the M in MVC.?the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.

? ? ? ?Active Record gives us several mechanisms, the most important being the ability to:1.Represent models and their data.2.Represent associations between these models.3.Represent inheritance hierarchies through related models.4.Validate models before they get persisted to the database.5. Perform database operations in an object-oriented fashion.

Convention over Configuration in Active Record

????????When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particularly true for ORM frameworks in general. However, if you follow the conventions adopted by Rails, you'll need to write very little configuration (in some cases no configuration at all) when creating Active Record models. The idea is that if you configure your applications in the very same way most of the time then this should be the default way. Thus, explicit configuration would be needed only in those cases where you can't follow the standard convention.

Naming Conventions

????????By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class?Book, you should have a database table called?books. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the CamelCase form.

Schema Conventions

? ??????Primary keys?- By default, Active Record will use an integer column named?id?as the table's primary key. When using Active Record Migrations?to create your tables, this column will be automatically created.

? ??????Foreign keys?- e.g.,?item_id,?order_id

Creating Active Record Models

????????class Product < ApplicationRecord

????????????????CREATE TABLE products (

???????????????????????id? int(11)? NOT NULL auto_increment,

???????????????????????name varchar(255),

???????????????????????PRIMARY? KEY? (id)

????????????????);

????????end

????????p = Product.new

????????p.name = "Some Book"

????????puts p.name ---- # "Some Book"

Overriding the Naming Conventions

? ? ? ?1. use your Rails application with a legacy database

????????????????class Product < ApplicationRecord

??????????????????????????self.table_name = "my_products"

? ? ? ? ? ? ? ? ?end

? ? ? ?2. used as the table's primary key using the?ActiveRecord::Base.primary_key

????????????????classProduct < ApplicationRecord

??????????????????????self.primary_key = "product_id"

????????????????end

CRUD: Reading and Writing Data

????????CRUD is an acronym for the four verbs we use to operate on data:?Create,?Read,?Update and?Delete.?

1 Create

? ???????The?new?method will return a new object while?create?will return the object and save it to the database.

? ? ? ? ?①user = User.create(name: "David", occupation: "Code Artist")

? ? ? ? ?②user = User.new

????????????user.name = "David"

????????????user.occupation = "Code Artist"

? ? ? ? ? ? user.save

2 Read

3 Update

4 Delete

Validations

????????Active Record allows you to validate the state of a model before it gets written into the database.?

Callbacks

????????Active Record callbacks allow you to attach code to certain events in the life-cycle of your models. This enables you to add behavior to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on.

Migrations

????????Rails provides a domain-specific language for managing a database schema called migrations. Migrations are stored in files which are executed against any database that Active Record supports using?rake. Here's a migration that creates a table:

????????Rails keeps track of which files have been committed to the database and provides rollback features. To actually create the table, you'd run?rails db:migrate?and to roll it back,?rails db:rollback.

????????Note that the above code is database-agnostic: it will run in MySQL, PostgreSQL, Oracle and others.?








user = User.create(name: "David", occupation: "Code Artist")

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容