Created
June 4, 2016 19:57
-
-
Save m-basov/ba34c0d9577ff4086839f95c5309695a to your computer and use it in GitHub Desktop.
Hanami foreig key example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student | |
include Hanami::Entity | |
attributes :teacher_id, … | |
end | |
class Teacher | |
include Hanami::Entity | |
attributes … | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collection :students do | |
attribute :id, Integer | |
attribute :teacher_id, Integer | |
… | |
end | |
collection :teachers do | |
attribute :id, Integer | |
… | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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