COMPSCI 169 Lecture Notes - Lecture 12: Referential Integrity, Foreign Key, Representational State Transfer
33 views2 pages
CS169: Software Engineering
October 14, 2013
❏Complete mid semester survey
❏Sign up for face to face project meetings with GSI
❏models must have attribute for foreign key
❏ActiveRecord manages it, not you!
❏For one-to-many association:
❏Add has_many to owning model and belongs_to to owned model
❏Create migration to add foreign key to owned side that references owning side
❏rake db:test:prepare to regenerate test database schema
❏but what about multiple associations?
❏add multiple “has_many” and “belongs_to”
❏but how would we get all the movies reviewed by a moviegoer?
RESTful Routes for Associations
❏how to keep track of movie and moviegoer for each review?
❏need info at creation time
❏use nested RESTful routes:
❏resources:movies becomes resources:moviesdoresources
❏access reviews by going “through” a movie
DRYing Out Queries with Reusable Scopes
❏stack scopes which will be evaluated lazily
❏associations are part of application architecture
❏can manipulate RDBMS foreign keys