Skip to Content
Learning Rails
book

Learning Rails

by Simon St. Laurent, Edd Dumbill
November 2008
Beginner
448 pages
12h 25m
English
O'Reilly Media, Inc.
Content preview from Learning Rails

Connecting Students to Awards

So, awards now have a basic understanding of the student records to which they connect. What can student records do with awards?

Removing Awards When Students Disappear

Although in reality, you might want to keep award listings around when students leave, for demonstration purposes it’s worth considering the problem of orphaned records. The validates_existence_of plug-in described earlier can check that a corresponding student record exists at the time the award record is created, but once the record has been created, validation doesn’t notice, for example, if the student is deleted. Keeping award records in sync with student records requires something more active.

Rails makes it very easy to make sure that when student records are deleted, the corresponding awards records are also deleted. You just need to add an option to the has_many declaration in app/models/student.rb:

has_many :awards, :dependent => :destroy

This is powerful and easy, but beware: those deletions will take place without any further confirmation. Once the user agrees to delete a student record, all of the awards records connected to that student will also disappear.

Counting Awards for Students

While adding the awards list to the main list of students could get really verbose, it does make sense to add a count of awards received to the list of students. If you add the set of awards listed in Figure B-3 of Appendix B, you’ll have an awards list like that shown in Figure 9-5.

Adding a count ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Rails 5

Learning Rails 5

J. Mark Locklear, Eric J Gruber, Barnabas Bulpett

Publisher Resources

ISBN: 9780596154943Errata