Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Commit b55d2ef

Browse files
authored
Merge pull request #18 from GraphQLAcademy/add-film-associations
Add Film associations and fixtures
2 parents e0ce8c4 + c15b01e commit b55d2ef

File tree

4 files changed

+496
-1
lines changed

4 files changed

+496
-1
lines changed

app/models/film.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
class Film < ApplicationRecord
2+
has_and_belongs_to_many :species
3+
has_and_belongs_to_many :starships
4+
has_and_belongs_to_many :vehicles
5+
has_and_belongs_to_many :planets
6+
has_and_belongs_to_many :characters,
7+
class_name: 'Person',
8+
join_table: 'films_people',
9+
foreign_key: 'film_id',
10+
association_foreign_key: 'person_id'
211
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class CreateFilmJoinTables < ActiveRecord::Migration[5.0]
2+
def change
3+
create_join_table :films, :species do |t|
4+
t.index :film_id
5+
t.index :species_id
6+
end
7+
8+
create_join_table :films, :starships do |t|
9+
t.index :film_id
10+
t.index :starship_id
11+
end
12+
13+
create_join_table :films, :vehicles do |t|
14+
t.index :film_id
15+
t.index :vehicle_id
16+
end
17+
18+
create_join_table :films, :people do |t|
19+
t.index :film_id
20+
t.index :person_id
21+
end
22+
23+
create_join_table :films, :planets do |t|
24+
t.index :film_id
25+
t.index :planet_id
26+
end
27+
end
28+
end

db/schema.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20170210220945) do
13+
ActiveRecord::Schema.define(version: 20170211182808) do
1414

1515
create_table "films", force: :cascade do |t|
1616
t.string "title"
@@ -23,6 +23,41 @@
2323
t.datetime "updated_at", null: false
2424
end
2525

26+
create_table "films_people", id: false, force: :cascade do |t|
27+
t.integer "film_id", null: false
28+
t.integer "person_id", null: false
29+
t.index ["film_id"], name: "index_films_people_on_film_id"
30+
t.index ["person_id"], name: "index_films_people_on_person_id"
31+
end
32+
33+
create_table "films_planets", id: false, force: :cascade do |t|
34+
t.integer "film_id", null: false
35+
t.integer "planet_id", null: false
36+
t.index ["film_id"], name: "index_films_planets_on_film_id"
37+
t.index ["planet_id"], name: "index_films_planets_on_planet_id"
38+
end
39+
40+
create_table "films_species", id: false, force: :cascade do |t|
41+
t.integer "film_id", null: false
42+
t.integer "species_id", null: false
43+
t.index ["film_id"], name: "index_films_species_on_film_id"
44+
t.index ["species_id"], name: "index_films_species_on_species_id"
45+
end
46+
47+
create_table "films_starships", id: false, force: :cascade do |t|
48+
t.integer "film_id", null: false
49+
t.integer "starship_id", null: false
50+
t.index ["film_id"], name: "index_films_starships_on_film_id"
51+
t.index ["starship_id"], name: "index_films_starships_on_starship_id"
52+
end
53+
54+
create_table "films_vehicles", id: false, force: :cascade do |t|
55+
t.integer "film_id", null: false
56+
t.integer "vehicle_id", null: false
57+
t.index ["film_id"], name: "index_films_vehicles_on_film_id"
58+
t.index ["vehicle_id"], name: "index_films_vehicles_on_vehicle_id"
59+
end
60+
2661
create_table "people", force: :cascade do |t|
2762
t.string "name"
2863
t.string "birth_year"

0 commit comments

Comments
 (0)