Skip to content

Commit e7a0bb6

Browse files
committed
add diagrams and code to clarify associations
1 parent f902451 commit e7a0bb6

File tree

9 files changed

+150
-13
lines changed

9 files changed

+150
-13
lines changed

class2.html

Lines changed: 150 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
<meta name="apple-mobile-web-app-capable" content="yes" />
1515
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
1616

17-
<link rel="stylesheet" href="reveal.js/css/reveal.css">
17+
<link rel="stylesheet" href="reveal.js/css/reveal.css">
1818
<link rel="stylesheet" href="reveal.js/css/theme/gdicool.css" id="theme">
1919
<link rel="stylesheet" href="overwrite.css">
2020

2121
<!-- For syntax highlighting -->
22-
<!-- light editor<link rel="stylesheet" href="lib/css/light.css">-->
23-
<!-- dark editor--><link rel="stylesheet" href="reveal.js/lib/css/dark.css">
22+
<link rel="stylesheet" href="reveal.js/lib/css/light.css">
23+
<!-- <link rel="stylesheet" href="reveal.js/lib/css/dark.css"> -->
2424

2525
<!-- If use the PDF print sheet so students can print slides-->
2626

2727
<link rel="stylesheet" href="reveal.js/css/print/pdf.css" type="text/css" media="print">
28-
28+
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
2929
</head>
3030

3131
<body>
@@ -144,7 +144,7 @@ <h3>Migrations</h3>
144144
<pre><code class="ruby">
145145
def change
146146
create_table :artists do |t|
147-
t.string :name
147+
t.string :full_name
148148
t.string :current_hairstyle
149149
t.timestamps
150150
end
@@ -168,19 +168,19 @@ <h3>Migrations</h3>
168168

169169
<section>
170170
<h3>Migrations</h3>
171-
<p>It would be pretty cool if songs has an attribute called 'optimal volume'. Let's create a migration to add that to the table. Run this in the terminal:</p>
171+
<p class="smalltext">It would be pretty cool if songs has an attribute called 'optimal volume'. Let's create a migration to add that to the table. Run this in the terminal:</p>
172172
<pre><code class="command-line">
173173
$ rails generate migration AddOptimalVolumetoSongs
174174
</code></pre>
175-
<p>And this will be the migration file it generates, with your additions:</p>
175+
<p class="smalltext">And this will be the migration file it generates, with your additions:</p>
176176
<pre><code class="ruby">
177177
class AddOptimalVolumetoSongs &lt; ActiveRecord::Migration
178178
def change
179179
add_column :songs, :optimal_volume, :string
180180
end
181181
end
182182
</code></pre>
183-
<p>Sweet. Now run the migration ('rake db:migrate') so the schema is up to date.</p>
183+
<p class="smalltext">Sweet. Now run the migration ('rake db:migrate') so the schema is up to date.</p>
184184
</section>
185185

186186
<section>
@@ -234,6 +234,12 @@ <h4>belongs_to</h4>
234234
</ul>
235235
</section>
236236

237+
<section>
238+
<h3>Data Associations</h3>
239+
<h4>belongs_to</h4>
240+
<a href="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-belongs_to-association"><img src="images/belongs_to.png" alt="belongs to association" /></a>
241+
</section>
242+
237243
<section>
238244
<h3>Data Associations</h3>
239245
<h4>has_one</h4>
@@ -246,6 +252,19 @@ <h4>has_one</h4>
246252
</ul>
247253
</section>
248254

255+
<section>
256+
<h3>Data Associations</h3>
257+
<h4>has_one</h4>
258+
<pre>
259+
<code>
260+
class Supplier < ActiveRecord::Base
261+
has_one :account
262+
end
263+
</code>
264+
</pre>
265+
<a href="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-has_one-association"><img src="images/has_one.png" alt="has one association" /></a>
266+
</section>
267+
249268
<section>
250269
<h3>Data Associations</h3>
251270
<h4>has_many</h4>
@@ -258,6 +277,19 @@ <h4>has_many</h4>
258277
</ul>
259278
</section>
260279

280+
<section>
281+
<h3>Data Associations</h3>
282+
<h4>has_many</h4>
283+
<pre>
284+
<code>
285+
class Customer < ActiveRecord::Base
286+
has_many :orders
287+
end
288+
</code>
289+
</pre>
290+
<a href="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-has_many-association"><img src="images/has_many.png" alt="has many association" /></a>
291+
</section>
292+
261293
<section>
262294
<h3>Data Associations</h3>
263295
<h4>has_many :through</h4>
@@ -271,24 +303,129 @@ <h4>has_many :through</h4>
271303

272304
<section>
273305
<h3>Data Associations</h3>
274-
<h4>Index?</h4>
306+
<h4>has_many :through</h4>
307+
<pre>
308+
<code>
309+
class Physician < ActiveRecord::Base
310+
has_many :appointments
311+
has_many :patients, :through => :appointments
312+
end
313+
314+
class Appointment < ActiveRecord::Base
315+
belongs_to :physician
316+
belongs_to :patient
317+
end
318+
319+
class Patient < ActiveRecord::Base
320+
has_many :appointments
321+
has_many :physicians, :through => :appointments
322+
end
323+
</code>
324+
</pre>
325+
</section>
326+
327+
<section>
328+
<h3>Data Associations</h3>
329+
<h4>has_many :through</h4>
330+
<a href="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-has_many-through-association"><img src="images/has_many_through.png" alt="has many through association" /></a>
331+
</section>
332+
333+
<section>
334+
<h3>Data Associations</h3>
335+
<h4>has_and_belongs_to_many</h4>
336+
<p class="smalltext">
337+
A has_and_belongs_to_many association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:
338+
</p>
339+
<pre>
340+
<code class="ruby">
341+
class Assembly < ActiveRecord::Base
342+
has_and_belongs_to_many :parts
343+
end
344+
345+
class Part < ActiveRecord::Base
346+
has_and_belongs_to_many :assemblies
347+
end
348+
</code>
349+
</pre>
350+
</section>
351+
352+
<section>
353+
<h3>Data Associations</h3>
354+
<h4>has_and_belongs_to_many (habtm)</h4>
355+
<a href="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-has_and_belongs_to_many-association"><img src="images/habtm.png" alt="has and belongs to many association" /></a>
356+
</section>
357+
358+
<section>
359+
<h3>Data Associations</h3>
360+
<h4>Choosing between has_many :through and habtm</h4>
361+
<br>
362+
<ol>
363+
<li>Set up a has_many :through relationship if:</li>
364+
<ul>
365+
<li>
366+
You need to work with the relationship model as an independent entity
367+
</li>
368+
</ul>
369+
<br>
370+
<li>Set up a has_and_belongs_to_many relationship if:</li>
371+
<ul>
372+
<li>
373+
You don’t need to do anything with the relationship model
374+
</li>
375+
</ul>
376+
</ol>
377+
<br><br>
378+
<p>
379+
You will need to remember to setup the intermediate table in the database
380+
</p>
381+
</section>
382+
383+
<section>
384+
<h3>Data Associations</h3>
385+
<h4>Index</h4>
275386
<p>A database index is a data structure that improves the speed of data retrieval at the cost of more storage space. An index is a copy of the selected columns, which can be searched very efficiently.</p>
276387
</section>
277388

389+
<section>
390+
<h3>Data Associations</h3>
391+
<h4>Index</h4>
392+
<img src="images/table_index_diagram.jpg" alt="table index diagram" />
393+
</section>
394+
278395
<section>
279396
<h3>Summary</h3>
280-
<p>Today, we planned out our application by writing user stories and discussing the architecture. Then, we generated the models that we'll be using, and hooked up the associations properly.</p>
397+
<p>Today we did this:</p>
398+
<ul>
399+
<li>Planned our application by writing user stories </li>
400+
<li>Discussing the architecture</li>
401+
<li>Then, we generated the model</li>
402+
<li>And hooked up the associations</li>
403+
</ul>
281404
</section>
282405

283406
<section>
284407
<h3>Questions?</h3>
285408
</section>
286409

287410
<section>
288-
<h3>Homework</h3>
411+
<h3>Homework 1</h3>
412+
<p class="smalltext"><a href="http://railsforzombies.org/levels/1">Complete Rails for Zombies</a>
413+
</p>
414+
<img src="images/rails_for_zombies.png" alt="rails for zombies logo" />
415+
</section>
416+
417+
<section>
418+
<h3>Homework 2</h3>
419+
<p>
420+
Use Rails Console with your Best Song App
421+
</p>
422+
<p>Try some commands</p>
289423
<ul>
290-
<li>Do the first three levels of <a href="http://railsforzombies.org/levels/1">Rails for Zombies</a></li>
291-
<li>Spend some time in the Rails Console with your Best Song App, creating, destroying, and updating database records.</li>
424+
<li>Create records</li>
425+
<li>Search for records</li>
426+
<li>Update record attributes</li>
427+
<li>Delete records</li>
428+
<li>Associate some record with another one</li>
292429
</ul>
293430
</section>
294431

images/belongs_to.png

33.2 KB
Loading

images/habtm.png

77.7 KB
Loading

images/has_many.png

53 KB
Loading

images/has_many_through.png

87.5 KB
Loading

images/has_one.png

53.3 KB
Loading

images/has_one_through.png

90.4 KB
Loading

images/rails_for_zombies.png

359 KB
Loading

images/table_index_diagram.jpg

41.7 KB
Loading

0 commit comments

Comments
 (0)