-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
342 lines (313 loc) · 15.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="author" content="Kedein Rodriguez Gatica" />
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">-->
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0">-->
<!--This one seems to work all the time, but really small on ipad-->
<!--<meta name="viewport" content="initial-scale=0.4">-->
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="theme/css/default.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="theme/css/phone.css">
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->
<script data-main="js/slides" src="js/require-1.0.8.min.js"></script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<slide class="logoslide nobackground">
<article class="flexbox vcenter">
<span><img src="images/rails.png"></span>
</article>
</slide>
<slide class="title-slide segue nobackground">
<aside class="gdbar"><img src="images/rails-icon.png"></aside>
<!-- The content of this hgroup is replaced programmatically through the slide_config.json. -->
<hgroup class="auto-fadein">
<h1 data-config-title><!-- populated from slide_config.json --></h1>
<h2 data-config-subtitle><!-- populated from slide_config.json --></h2>
<p data-config-presenter><!-- populated from slide_config.json --></p>
</hgroup>
</slide>
<slide>
<hgroup>
<h2>1 What is Active Record?</h2>
</hgroup>
<article>
<p>Active Record is the <span class="red">M</span> in MVC - The model</p>
<ul class="build">
<li>Is the layer of the system responsible for representing business data and logic.</li>
<li>Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.</li>
<li>It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>1.1 The Active Record Pattern</h2>
</hgroup>
<article>
<p> In Active Record, objects carry both persistent data and behavior which operates on that data. </p>
<p>Active Record takes the opinion that ensuring data access logic as part of the object will educate users of that object on how to write to and read from the database.</p>
</article>
</slide>
<slide>
<hgroup>
<h2>1.2 Object Relational Mapping</h2>
</hgroup>
<article>
<p>ORM, is a technique that connects the rich objects of an application to tables in a relational database management system. </p>
<p>Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code.</p>
</article>
</slide>
<slide>
<hgroup>
<h2>1.3 Active Record as an ORM Framework</h2>
</hgroup>
<article>
<p>Active Record gives us several mechanisms, the most important being the ability to: </p>
<ul class="build">
<li>Represent models and their data.</li>
<li>Represent associations between these models.</li>
<li>Represent inheritance hierarchies through related models.</li>
<li>Validate models before they get persisted to the database.</li>
<li>Perform database operations in an object-oriented fashion.</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>2 Convention over Configuration in Active Record</h2>
</hgroup>
<article>
<p>When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. </p>
<p>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.</p>
</article>
</slide>
<slide>
<hgroup>
<h2>2.1 Naming Conventions</h2>
</hgroup>
<article>
<ul class="build">
<li>Model Class - Singular with the first letter of each word capitalized (e.g., BookClub).</li>
<li>Database Table - Plural with underscores separating words (e.g., book_clubs).</li>
</ul>
<table>
<tr>
<th>Model / Class</th><th>Table / Schema</th>
</tr>
<tr>
<td>Article</td><td>articles</td>
</tr>
<tr>
<td>LineItem</td><td>line_items</td>
</tr>
<tr>
<td>Deer</td><td>deers</td>
</tr>
<tr>
<td>Mouse</td><td>mice</td>
</tr>
<tr>
<td>Person</td><td>people</td>
</tr>
</table>
</article>
</slide>
<slide>
<hgroup>
<h2>2.2 Schema Conventions</h2>
</hgroup>
<article>
<p>Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.</p>
<ul class="build">
<li>Foreign keys
<p>singularized_table_name_id (e.g., item_id, order_id)</p>
</li>
<li>Primary keys
<p>This column will be automatically created.</p>
<p>(bigint for PostgreSQL and MySQL, integer for SQLite)</p>
<p>|bigint |8 bytes | large-range integer | -9223372036854775808 to 9223372036854775807|</p>
</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>3 Creating Active Record Models</h2>
</hgroup>
<article>
<p>It is very easy to create Active Record models. All you have to do is to subclass the ApplicationRecord class and you're good to go:</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=164%25&si=false&es=4x&wm=false&code=class%2520Product%2520%253C%2520ApplicationRecord%250Aend"
style="width: 508px; height: 244px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide>
<hgroup>
</hgroup>
<article>
<p>Suppose that the products table was created using an SQL (or one of its extensions) statement like:</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=164%25&si=false&es=4x&wm=false&code=CREATE%2520TABLE%2520products%2520%28%250A%2520%2520%2520id%2520int%2811%29%2520NOT%2520NULL%2520auto_increment%252C%250A%2520%2520%2520name%2520varchar%28255%29%252C%250A%2520%2520%2520PRIMARY%2520KEY%2520%2520%28id%29%250A%29%253B"
style="width: 562px; height: 330px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide>
<hgroup>
</hgroup>
<article>
<p>Schema above declares a table with two columns: id and name. Each row of this table represents a certain product with these two parameters. Thus, you would be able to write code like the following:</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=164%25&si=false&es=4x&wm=false&code=p%2520%253D%2520Product.new%250Ap.name%2520%253D%2520%2522Some%2520Book%2522%250Aputs%2520p.name%2520%2523%2520%2522Some%2520Book%2522"
style="width: 421px; height: 273px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide>
<hgroup>
<h2>4 Overriding the Naming Conventions</h2>
</hgroup>
<article>
<p>What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.</p>
</article>
</slide>
<slide class="segue dark nobackground">
<aside class="gdbar"><img src="images/rails-icon.png"></aside>
<hgroup class="auto-fadein">
<h2>5 CRUD: Reading and Writing Data</h2>
<h3>Create, Read, Update and Delete</h3>
<p>Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables.</p>
</hgroup>
</slide>
<slide>
<hgroup>
<h2>5.1 Create</h2>
</hgroup>
<article>
<p>The <span class="blue">create</span> method call will create and save a new record into the database:</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=161%25&si=false&es=4x&wm=false&code=user%2520%253D%2520User.create%28name%253A%2520%2522David%2522%252C%2520occupation%253A%2520%2522Code%2520Artist%2522%29"
style="width: 900px; height: 415px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide>
<hgroup>
</hgroup>
<article>
<p>Using the <span class="blue">new</span> method, an object can be instantiated without being saved:</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=161%25&si=false&es=4x&wm=false&code=user%2520%253D%2520User.new%250Auser.name%2520%253D%2520%2522David%2522%250Auser.occupation%2520%253D%2520%2522Code%2520Artist%2522"
style="width: 486px; height: 373px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
<p>A call to <span class="blue">user.save</span> will commit the record to the database.</p>
</article>
</slide>
<slide>
<hgroup>
<h2>5.2 Read</h2>
</hgroup>
<article>
<p>Active Record provides a rich API for accessing data within a database.</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=161%25&si=false&es=4x&wm=false&code=%2523%2520return%2520a%2520collection%2520with%2520all%2520users%250Ausers%2520%253D%2520User.all%250A%250A%2523%2520return%2520the%2520first%2520user%250Auser%2520%253D%2520User.first%250A%250A%2523%2520return%2520the%2520first%2520user%2520named%2520David%250Adavid%2520%253D%2520User.find_by%28name%253A%2520%27David%27%29%250A%250A%2523%2520find%2520all%2520users%2520named%2520David%2520who%2520are%2520Code%2520Artists%2520and%2520sort%2520by%2520created_at%2520in%2520reverse%2520chronological%2520order%250Ausers%2520%253D%2520User.where%28name%253A%2520%27David%27%252C%2520occupation%253A%2520%27Code%2520Artist%27%29.order%28created_at%253A%2520%253Adesc%29"
style="width: 1074px; height: 561px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide>
<hgroup>
<h2>5.3 Update</h2>
</hgroup>
<article>
<p>Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database.</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=161%25&si=false&es=4x&wm=false&code=user%2520%253D%2520User.find_by%28name%253A%2520%27David%27%29%250Auser.name%2520%253D%2520%27Dave%27%250Auser.save"
style="width: 718px; height: 473px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide>
<hgroup>
</hgroup>
<article>
<p>A shorthand for this is to use a hash mapping attribute names to the desired value, like so:</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=161%25&si=false&es=4x&wm=false&code=user%2520%253D%2520User.find_by%28name%253A%2520%27David%27%29%250Auser.update%28name%253A%2520%27Dave%27%29"
style="width: 718px; height: 444px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide>
<hgroup>
<h2>5.4 Delete</h2>
</hgroup>
<article>
<p>Likewise, once retrieved an Active Record object can be destroyed which removes it from the database.</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=161%25&si=false&es=4x&wm=false&code=user%2520%253D%2520User.find_by%28name%253A%2520%27David%27%29%250Auser.destroy"
style="width: 718px; height: 444px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide>
<hgroup>
</hgroup>
<article>
<p>If you'd like to delete several records in bulk, you may use <span class="blue">destroy_by</span> or <span class="blue">destroy_all</span> method:</p>
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28255%2C255%2C255%2C1%29&t=seti&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=18px&lh=161%25&si=false&es=4x&wm=false&code=%2523%2520find%2520and%2520delete%2520all%2520users%2520named%2520David%250AUser.destroy_by%28name%253A%2520%27David%27%29%250A%2520%250A%2523%2520delete%2520all%2520users%250AUser.destroy_all"
style="width: 573px; height: 630px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</article>
</slide>
<slide class="thank-you-slide segue nobackground">
<aside class="gdbar right"><img src="images/rails-icon.png"></aside>
<article class="flexbox vleft auto-fadein">
<h2>
<p>Thank You 😼</p>
</h2>
</article>
<p class="auto-fadein" data-config-contact>
<!-- populated from slide_config.json -->
</p>
</slide>
<slide class="logoslide dark nobackground">
<article class="flexbox vcenter">
<span><img src="images/rails.png"></span>
</article>
</slide>
<slide class="backdrop"></slide>
</slides>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!--[if IE]>
<script src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js"></script>
<script>CFInstall.check({mode: 'overlay'});</script>
<![endif]-->
</body>
</html>