Created
January 9, 2019 18:09
-
-
Save russ/e6353ac39b0c7b0322f336a47c273b7e to your computer and use it in GitHub Desktop.
Clear Compilation Error
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
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); |
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
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