Skip to content

Instantly share code, notes, and snippets.

@data-doge
Created July 22, 2015 03:18
Show Gist options
  • Select an option

  • Save data-doge/95f127cb34736906a38b to your computer and use it in GitHub Desktop.

Select an option

Save data-doge/95f127cb34736906a38b to your computer and use it in GitHub Desktop.
require_relative '../config'
class CreateStudents < ActiveRecord::Migration
def change
create_table :students do |t|
t.string :first_name
t.string :last_name
t.string :gender
t.date :birthday
t.string :email
t.string :phone
t.timestamps null: false
end
end
end
require_relative '../config'
class CreateTeachers < ActiveRecord::Migration
create_table :teachers do |t|
t.string :name
t.timestamps null: false
end
end
require_relative "./../config.rb"
class CreateStudentTeachers < ActiveRecord::Migration
create_table :student_teachers do |t|
t.belongs_to :student
t.belongs_to :teacher
t.timestamps null: false
end
end
our ideal world:
teachers
---------------------
id name students
1 "asdf" [<student object>,<student object>,<student object>, ...]
teacher = Teacher.find(1)
teacher.students -> an array of student object
our ideal world doesn't exist - but join tables do!
students
----------
id name
1 yasha
2 kashi
3 barry
4 pesto-basil
teachers
----------
id name
1 eugene
2 charles
assuming we have a situation like this:
eugene has a class with:
yasha
pesto-basil
barry
charles has a class with:
kashi
pesto-basil
our join table might look like this:
student_teachers
----------------------------
id student_id teacher_id
1 1 1
2 4 1
3 3 1
4 2 2
5 4 2
^^ so yeah, this is a join table. its a table of relationships between objects. two sets of ids.
with activerecord, we can set up a join table and some nice interfaces for our models that allow us to do things like:
teacher = Teacher.find(1) # -> <Teacher name: eugene>
teacher.students -> an array of student objects found from the join table (wherever teacher_id was equal to this teacher's id)
similarly...
student = Student.find(1)
student.teachers -> returns an array of teachers that the student is associated with in the join table
*~*~*~*~*~*~**~**~*~*~
other examples of join tables
messages
--------------------------------------------------------------
sender_id recipient_id body subject timestamps
transactions
--------------------------------------------------
sender_id recipient_id amount timestamps
require_relative '../../db/config'
class Student < ActiveRecord::Base
before_save :normalize_name, if: :has_name?
has_many :student_teachers
has_many :teachers, through: :student_teachers
def name
"#{first_name} #{last_name}"
end
def to_s
[first_name, last_name, gender, birthday].compact.join(' | ')
end
private
def normalize_name
first_name.strip!
last_name.strip!
first_name.capitalize!
last_name.capitalize!
end
def has_name?
first_name.present? && last_name.present?
end
end
require_relative '../../db/config'
class StudentTeacher < ActiveRecord::Base
belongs_to :student
belongs_to :teacher
end
require_relative '../../db/config'
class Teacher < ActiveRecord::Base
has_many :student_teachers
has_many :students, through: :student_teachers
end
require_relative "app/models/student.rb"
require_relative "app/models/teacher.rb"
require_relative "app/models/student_teacher.rb"
teacher = Teacher.create(name: "asdfasdfa")
5.times { puts }
teacher.students << Student.create(first_name: "eugene", last_name: "lynch")
p teacher.students
5.times { puts }
student = Student.find_by_first_name("Eugene")
p student.teachers
5.times { puts }
p StudentTeacher.first
p StudentTeacher.first.student
p StudentTeacher.first.teacher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment