-
Imagine that you have Students and Addresses. A student can have many addresses (ie - current address, permanent address, etc.), and an address belongs to one student. Create migrations (
rails g migration ...
) for both students and addresses. -
Create a migration to add a boolean column
is_alumnus
to the students table. Give this a default value of false. Migrate this and watch howschema.rb
changed. -
Rollback one previous migration. What changed? Can you re-migrate so that
is_alumnus
is in your table again?
-
Create a Student model that inherits from ActiveRecord::Base (do this by hand -- don't use a generator for this)
-
Start
rails console
from the command line. Check how many class methods are available on the Student class withStudent.methods.count
-
Create a new instance of a Student (
student = Student.new
). Check how many methods are available usingstudent.methods.count
. -
Create a plain Ruby class and check how many methods are availble both for the class and an instance of the class.
-
Use the instance methods
new_record?
,update_attributes
, andsave
onstudent
. What do they do? How are they used? Reference the docs if you're stuck. -
Use the class methods
all
to see all Students andfind
/find_by
to locate a student by an id or other attribute. -
Set up a one-to-many relationship between the Address and Student models. (You'll first need to create the Address model if you haven't already).
-
In the
rails console
, create a new Student (student = Student.new(...)
), save that student (student.save
), and create a new address for that student (student.addresses.create(...)
). -
Look at the ActiveRecord relationship with
student.addresses
.
Use our routes and controllers lesson from yesterday to add functionality so that:
- when visiting
'/students'
, text is rendered with the names of all of the students - when visiting
'/students/:id'
, text is rendered showing the student's name and a list of that student's addresses.
- Imagine that you have Students, Courses, and Enrollments. A student has many courses, and a course has many students. The many-to-many relationship is created through the join table 'enrollments'. You should already have a migration for students. Create two more migrations: courses and enrollments.
-
Set up a many-to-many relationship with Students, Courses, and Enrollments
-
In the
rails console
, create a new Student (student = Student.new
) and save that student (student.save
). Create a new course (course = Course.new
) and save it (course.save
). Create an enrollment withstudent.enrollments.create(
course_id: course.id`). -
Look at the ActiveRecord relationships with
student.enrollments
andstudent.courses
.
Use our routes and controllers lesson from yesterday to add functionality so that:
- when visiting
'/courses'
, text is rendered showing all of the course names - when visiting
'/courses/:id'
, text is rendered to show the course name and a list of all enrolled students - add functionality to
'/students/:id'
so that in addition to name and addresses, all of that student's course names are also displayed