You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<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
+
<pclass="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>
<p>And this will be the migration file it generates, with your additions:</p>
175
+
<pclass="smalltext">And this will be the migration file it generates, with your additions:</p>
176
176
<pre><codeclass="ruby">
177
177
class AddOptimalVolumetoSongs < ActiveRecord::Migration
178
178
def change
179
179
add_column :songs, :optimal_volume, :string
180
180
end
181
181
end
182
182
</code></pre>
183
-
<p>Sweet. Now run the migration ('rake db:migrate') so the schema is up to date.</p>
183
+
<pclass="smalltext">Sweet. Now run the migration ('rake db:migrate') so the schema is up to date.</p>
184
184
</section>
185
185
186
186
<section>
@@ -234,6 +234,12 @@ <h4>belongs_to</h4>
234
234
</ul>
235
235
</section>
236
236
237
+
<section>
238
+
<h3>Data Associations</h3>
239
+
<h4>belongs_to</h4>
240
+
<ahref="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-belongs_to-association"><imgsrc="images/belongs_to.png" alt="belongs to association" /></a>
241
+
</section>
242
+
237
243
<section>
238
244
<h3>Data Associations</h3>
239
245
<h4>has_one</h4>
@@ -246,6 +252,19 @@ <h4>has_one</h4>
246
252
</ul>
247
253
</section>
248
254
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
+
<ahref="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-has_one-association"><imgsrc="images/has_one.png" alt="has one association" /></a>
266
+
</section>
267
+
249
268
<section>
250
269
<h3>Data Associations</h3>
251
270
<h4>has_many</h4>
@@ -258,6 +277,19 @@ <h4>has_many</h4>
258
277
</ul>
259
278
</section>
260
279
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
+
<ahref="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-has_many-association"><imgsrc="images/has_many.png" alt="has many association" /></a>
291
+
</section>
292
+
261
293
<section>
262
294
<h3>Data Associations</h3>
263
295
<h4>has_many :through</h4>
@@ -271,24 +303,129 @@ <h4>has_many :through</h4>
271
303
272
304
<section>
273
305
<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
+
classPatient<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
+
<ahref="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-has_many-through-association"><imgsrc="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
+
<pclass="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
+
<codeclass="ruby">
341
+
class Assembly <ActiveRecord::Base
342
+
has_and_belongs_to_many:parts
343
+
end
344
+
345
+
classPart<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
+
<ahref="http://guides.rubyonrails.org/v3.2.21/association_basics.html#the-has_and_belongs_to_many-association"><imgsrc="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>
275
386
<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>
276
387
</section>
277
388
389
+
<section>
390
+
<h3>Data Associations</h3>
391
+
<h4>Index</h4>
392
+
<imgsrc="images/table_index_diagram.jpg" alt="table index diagram" />
393
+
</section>
394
+
278
395
<section>
279
396
<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>
281
404
</section>
282
405
283
406
<section>
284
407
<h3>Questions?</h3>
285
408
</section>
286
409
287
410
<section>
288
-
<h3>Homework</h3>
411
+
<h3>Homework 1</h3>
412
+
<pclass="smalltext"><ahref="http://railsforzombies.org/levels/1">Complete Rails for Zombies</a>
413
+
</p>
414
+
<imgsrc="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>
289
423
<ul>
290
-
<li>Do the first three levels of <ahref="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>
0 commit comments