Skip to content

Commit

Permalink
Test through through
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed May 19, 2022
1 parent 67d67a0 commit d748a7e
Show file tree
Hide file tree
Showing 7 changed files with 1,459 additions and 221 deletions.
11 changes: 11 additions & 0 deletions app/models/city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class City < ApplicationRecord
include ChronoModel::TimeMachine

validates :name, presence: true

belongs_to :country

has_many :schools, dependent: :destroy
end
11 changes: 11 additions & 0 deletions app/models/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Country < ApplicationRecord
include ChronoModel::TimeMachine

has_many :cities, dependent: :destroy
has_many :schools, through: :cities
has_many :students, through: :schools

validates :name, presence: true
end
12 changes: 12 additions & 0 deletions app/models/school.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class School < ApplicationRecord
include ChronoModel::TimeMachine

belongs_to :city
has_one :country, through: :city

has_many :students, dependent: :destroy

validates :name, presence: true
end
11 changes: 11 additions & 0 deletions app/models/student.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Student < ApplicationRecord
include ChronoModel::TimeMachine

belongs_to :school
has_one :city, through: :school
has_one :country, through: :city

validates :name, presence: true
end
33 changes: 33 additions & 0 deletions db/migrate/20220519102012_create_schools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class CreateSchools < ActiveRecord::Migration[6.1]
def change
create_table :countries, temporal: true do |t|
t.string :name

t.timestamps
end

create_table :cities, temporal: true do |t|
t.references :country

t.string :name

t.timestamps
end

create_table :schools, temporal: true do |t|
t.references :city

t.string :name

t.timestamps
end

create_table :students, temporal: true do |t|
t.references :school

t.string :name

t.timestamps
end
end
end
28 changes: 8 additions & 20 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,14 @@
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

user = User.create! name: 'John Doe'
project = user.projects.create! name: 'Project 1'
unit_a = project.units.create! name: 'Unit A 10 days ago'
france = Country.create!(name: 'France')
italy = Country.create!(name: 'Italy')

puts "User, Project, Unit A #{Time.current.iso8601}"
paris = france.cities.create!(name: 'Paris')
rome = italy.cities.create!(name: 'Rome')

sleep 3.seconds
pascal = paris.schools.create!(name: 'B. Pascal')
galilei = rome.schools.create!(name: 'G. Galilei')

unit_b = project.units.create! name: 'Unit B'

puts "Unit B #{Time.current.iso8601}"

sleep 3.seconds

unit_a.update! name: 'Unit A 5 days ago'

puts "Unit A updated #{Time.current.iso8601}"

sleep 3.seconds

user.update! name: 'Joe Doe'

puts "User updated #{Time.current.iso8601}"
pascal.students.create!(name: 'Vincent')
galilei.students.create!(name: 'Mario')
Loading

0 comments on commit d748a7e

Please sign in to comment.