AirJD 焦点
AirJD

没有录音文件
00:00/00:00
加收藏

从数据库记录到 ActiveRecord 对象 by 陈恺

发布者 rubyconf
发布于 1430111630678  浏览 5043 关键词 Ruby 
分享到

第1页

Row => Ruby Object

Explore ActiveRecord Source Code



第2页

Me



第3页

陈恺

已婚已育的程序员



第4页

Work at SmartLion

Team Lead



第5页

• @_kaichen at Twitter • @kaichen at Github • kai@thekaiway.com



第6页

Agenda



第7页

Talk is cheap, show me the code.

- Linus Torvalds



第8页

Tell you(我跟你讲啊)

• Think More • Show some Respect • English Reading from Nice Doc Comment • Help you debug



第9页

Manner

• Give you a Overview First • Show you the way of thinking • Show you the Skills • Guide you dig down code



第10页

Content

• Database • Rails • Trace Ruby Code • ActiveRecord • Sample • Q&A



第11页

Target

• Junior Rails Developers • 2+ years



第12页

Goal

• Learn Internal of ActiveRecord • Interest about Computer Science



第13页

Find Out Together

Read and Make Fun Together, and I don’t have idea how long of this workshop



第15页

Database



第16页

Database

• Relation • SQL • Runtime Model • IO Model/Storage



第17页

Relational



第18页

Relational



第19页

Relational



第20页

SQL



第21页

SQL

• SELECT * FROM users WHERE users.id = 1 limit 1; • INSERT INTO users (name, email) VALUES (‘kai’,

‘kai@thekaiway.com’); • UPDATE users SET age = age + 1; • DETELE FROM users WHERE email =

‘kai@thekaiway.com’;



第22页

Runtime

• Serverless • Multi-Threads • Multi-Processes



第23页

Memory Management & I/O

• Memory • Save to Disk



第24页

Rails



第25页

Ruby on Rails

Full Stack Web Framework



第26页

Rails



ActionView



ActionMailer ActiveModel



ActionPack ActiveRecord



ActiveSupport



第27页

Rails



ActiveRecord



ActiveModel



ActionMailer



ActionPack



ActionView



Sprockets



ActiveSupport



第28页

Follow Rails Changes

• https://github.com/rails/rails • USING Git!!!



第29页

Trace Ruby Code



第30页

Trace Ruby Code

• Search Source Code • Debug in Runtime • Proof in Sample/Production Project



第31页

Searching Source Code

• Grep - http://www.gnu.org/software/grep/ • Ack - https://github.com/petdance/ack2 • Ag - https://github.com/ggreer/the_silver_searcher • Pt - https://github.com/monochromegane/

the_platinum_searcher



第32页

Runtime Debugging

• puts / inspect • pry • Raise Error



第33页

Sample - Live Coding



第34页

New Rules



第35页

No more magic



第36页

Less Dynamic Method Define



第37页

No code faster than code.

–Jeff Atwood



第38页

ActiveRecord



第39页

The Pattern

From Martin Fowler’s 「Patterns of Enterprise Application Architecture」



第40页

ActiveRecord Pattern

• Database Row presents as an Ruby Object • Class maps to a Table • Instance maps to a row • CRUD on an Instance perform SQL Query



第41页

Features



Migration



Column Value Typecasting



Association Identity Map



Schema Dump Fixtures



Prepare Statement



Connection Pool Query Cache



Connection Reaper



Timestamp



DDL Generation Session Store



Transaction STI Lock



Query Explain



Enum Column SQL Sanitization



Statement Cache



Callbacks Validations



第42页

Let’s Start

Using Rails 4.2.0.beta1



第43页

Base Structure

Live!!!



第44页

Code Structure

• Under Rails activerecord dir • Read ruby gem spec • Explore lib dir



第45页

Database Connection



第46页

Database Connection

• Connection Pool • Reap • Query Cache • Transaction



第47页

Connection Pool

• Multi-Thread



第48页

Schema



第49页

Schema

• db/schema.rb • Column-Attribute Mapping



第50页

Assemble Object



第51页

ActiveRecord Object

• Create Manually • Create from Query



第52页

States

• Schema • Attributes • Is New Record? • Did it Destroy? • Errors from validation



第53页

SQL



第54页

SQL

• Arel • DDL



第55页

Arel



第56页

Arel



第57页

Arel



第58页

Arel



第59页

Model Association



第60页

Model Association

• belongs_to • has_many • has_one • has_and_blongs_to_many



第61页

Living Coding

• Trace Migration • Find out how Migration works



第62页

How to keep learning



第64页

Recruit



第65页

Recruit

• Rails Developers x 2 • iOS/Android Developers x 2 • Product Manager x 1 • UI Designer x1



第66页

END



支持文件格式:*.pdf
上传最后阶段需要进行在线转换,可能需要1~2分钟,请耐心等待。