Skip to content
Fernando Correa de Oliveira edited this page Jul 7, 2020 · 2 revisions

model Person { ... }

model Track { has UInt $!id is serial; has UInt $!person-id is referencing( *.name, :model ); has Person $.person is relationship{ .person-id }; has Instant $.time is column; }

model Person { has Str $.name is id; has @.tracks is relationship( *.person-id, :model);

method total { @!tracks.sort(*.time).batch(2).map({ .[1].time - .[0].time }).sum } }

my $*RED-DB = database "SQLite";

Person.^create-table; Track.^create-table;

my $*RED-DEBUG = True;

my \fernando = Person.^create: :name; fernando.tracks.create: :time(now - 2000); fernando.tracks.create: :time(now - 1000); fernando.tracks.create: :time(now - 500); fernando.tracks.create: :time(now);

say fernando.total