Skip to content

Instantly share code, notes, and snippets.

@m-basov
Created June 4, 2016 19:57
Show Gist options
  • Save m-basov/ba34c0d9577ff4086839f95c5309695a to your computer and use it in GitHub Desktop.
Save m-basov/ba34c0d9577ff4086839f95c5309695a to your computer and use it in GitHub Desktop.
Hanami foreig key example
class Student
include Hanami::Entity
attributes :teacher_id, …
end
class Teacher
include Hanami::Entity
attributes …
end
collection :students do
attribute :id, Integer
attribute :teacher_id, Integer
end
collection :teachers do
attribute :id, Integer
end
Hanami::Model.migration do
change do
create_table :students do
primary_key :id
foreign_key :teacher_id, :teachers, null: false
end
create_table :teachers do
primary_key :id
end
end
end
teacher = TeacherRepository.new.create(Teacher.new)
student = Student.new(teacher_id: teacher.id)
StudentRepository.new.create(student)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment