To Lecture Notes
IT 232 -- 10/14/15
Review Exercises
- What does the spaceship operator have to do with sorting objects?
Ans: If
<=> is defined for a model, then an array of objects from that model can be
sorted with the Array sort method.
- How does has_and_belongs_to_many work?
Ans: It defines a many-to-many relationship. For example, if a many-to-many relationship
is defined for Student and Course using the table courses_students, then if s is a student
object, s.courses returns an array of all of the courses in which the student s is enrolled. Similarly,
if c is a course object, c.students returns an array of the students enrolled in the course c.
Project 3
- Suggestions for Project 3 models:
- Owner, Photo, Like. Owner is the same as follower
- Owner, Follower, Photo, Like. Set up a one-to-one relationship between Owner and Follower.
- Owner, Follower, Photo. Use a followers_photos table (many-to-many-relationship).
- Follower, Photo, Like. Recommended. Similar to the School1 Example.
More Examples
- Look at the LikesSite and LinkIcons Examples.
Using ActiveRecord
- ActiveRecord Queries
- Practice Problem: Set up a scaffold-based project with model Photo,
fields description, and file_name. Write a loop that displays all of the photos in a view
in order of most recent to least recent. Use these photos for your project: dogs-owners.zip.
Ans: See the PhotoGallery Example.