Skip to content

Instantly share code, notes, and snippets.

@russ
Created January 9, 2019 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russ/e6353ac39b0c7b0322f336a47c273b7e to your computer and use it in GitHub Desktop.
Save russ/e6353ac39b0c7b0322f336a47c273b7e to your computer and use it in GitHub Desktop.
Clear Compilation Error
DROP TABLE videos;
DROP TABLE video_releases;
DROP TABLE actors;
DROP TABLE performances;
CREATE TABLE videos (
id integer NOT NULL,
title varchar(40) NOT NULL
);
CREATE TABLE video_releases (
id integer NOT NULL,
releaseable_id integer NOT NULL,
title varchar(40) NOT NULL
);
CREATE TABLE actors (
id integer NOT NULL,
name varchar(40) NOT NULL
);
CREATE TABLE performances (
video_id integer NOT NULL,
actor_id integer NOT NULL
);
INSERT INTO videos VALUES (1, 'Video Title');
INSERT INTO video_releases VALUES (1, 1, 'Video Release');
INSERT INTO actors VALUES (1, 'Joe Bob');
INSERT INTO performances VALUES (1, 1);
require "clear"
Clear::SQL.init("postgres://russ@localhost:5432/association-test")
class Video
include Clear::Model
self.table = "videos"
with_serial_pkey
column id : Int64, primary: true
column title : String
has_many performances : Performance, foreign_key: :video_id
has_many actors : Actor, through: Performance, foreign_key: :actor_id, own_key: :video_id
has_many video_releases : VideoRelease, foreign_key: :releaseable_id
end
class VideoRelease
include Clear::Model
self.table = "video_releases"
with_serial_pkey
column id : Int64, primary: true
column title : String
belongs_to video : Video, foreign_key: :releaseable_id
end
class Actor
include Clear::Model
self.table = "actors"
with_serial_pkey
column id : Int64, primary: true
column name : String
has_many performances : Performance, foreign_key: :actor_id
has_many videos : Video, through: Performance, foreign_key: :video_id, own_key: :actor_id
end
class Performance
include Clear::Model
self.table = "performances"
belongs_to actor : Actor
belongs_to video : Video, primary: true
end
puts VideoRelease.query.with_video(&.with_actors).first.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment