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

Commit b1cd1b2

Browse files
authored
Merge pull request #12 from GraphQLAcademy/add-people-vehicles-join
Add Vehicle#pilots, Person#vehicles associations and fixtures
2 parents 9f7434d + 9ecda7e commit b1cd1b2

6 files changed

Lines changed: 69 additions & 1 deletion

File tree

app/models/person.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ class Person < ApplicationRecord
22
belongs_to :homeworld, class_name: 'Planet'
33

44
has_and_belongs_to_many :starships
5+
has_and_belongs_to_many :vehicles
56
end

app/models/vehicle.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
class Vehicle < ApplicationRecord
2+
has_and_belongs_to_many :pilots,
3+
class_name: 'Person',
4+
join_table: 'people_vehicles',
5+
foreign_key: 'vehicle_id',
6+
association_foreign_key: 'person_id'
27
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CreateJoinTableVehiclesPeople < ActiveRecord::Migration[5.0]
2+
def change
3+
create_join_table :vehicles, :people do |t|
4+
t.index :vehicle_id
5+
t.index :person_id
6+
end
7+
end
8+
end

db/schema.rb

Lines changed: 8 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: 20170209014741) do
13+
ActiveRecord::Schema.define(version: 20170209173854) do
1414

1515
create_table "people", force: :cascade do |t|
1616
t.string "name"
@@ -34,6 +34,13 @@
3434
t.index ["starship_id"], name: "index_people_starships_on_starship_id"
3535
end
3636

37+
create_table "people_vehicles", id: false, force: :cascade do |t|
38+
t.integer "vehicle_id", null: false
39+
t.integer "person_id", null: false
40+
t.index ["person_id"], name: "index_people_vehicles_on_person_id"
41+
t.index ["vehicle_id"], name: "index_people_vehicles_on_vehicle_id"
42+
end
43+
3744
create_table "planets", force: :cascade do |t|
3845
t.string "name"
3946
t.integer "diameter"

test/fixtures/people.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ luke-skywalker:
1010
starships:
1111
- x-wing
1212
- imperial-shuttle
13+
vehicles:
14+
- snowspeeder
15+
- imperial-speeder-bike
1316
homeworld: tatooine
1417
created_at: '2014-12-09T13:50:51.644000Z'
1518
updated_at: '2014-12-20T21:17:56.891000Z'
@@ -64,6 +67,8 @@ leia-organa:
6467
height: '150'
6568
mass: '49'
6669
skin_color: light
70+
vehicles:
71+
- imperial-speeder-bike
6772
homeworld: alderaan
6873
created_at: '2014-12-10T15:20:09.791000Z'
6974
updated_at: '2014-12-20T21:17:50.315000Z'
@@ -136,6 +141,8 @@ obi-wan-kenobi:
136141
- naboo-star-skiff
137142
- jedi-interceptor
138143
- belbullab-22-starfighter
144+
vehicles:
145+
- tribubble-bongo
139146
homeworld: stewjon
140147
created_at: '2014-12-10T16:16:29.192000Z'
141148
updated_at: '2014-12-20T21:17:50.325000Z'
@@ -153,6 +160,9 @@ anakin-skywalker:
153160
- trade-federation-cruiser
154161
- jedi-interceptor
155162
- naboo-fighter
163+
vehicles:
164+
- zephyr-g-swoop-bike
165+
- xj-6-airspeeder
156166
homeworld: tatooine
157167
created_at: '2014-12-10T16:20:44.310000Z'
158168
updated_at: '2014-12-20T21:17:50.327000Z'
@@ -180,6 +190,8 @@ chewbacca:
180190
starships:
181191
- millennium-falcon
182192
- imperial-shuttle
193+
vehicles:
194+
- at-st
183195
homeworld: kashyyyk
184196
created_at: '2014-12-10T16:42:45.066000Z'
185197
updated_at: '2014-12-20T21:17:50.332000Z'
@@ -237,6 +249,8 @@ wedge-antilles:
237249
skin_color: fair
238250
starships:
239251
- x-wing
252+
vehicles:
253+
- snowspeeder
240254
homeworld: corellia
241255
created_at: '2014-12-12T11:08:06.469000Z'
242256
updated_at: '2014-12-20T21:17:50.341000Z'
@@ -420,6 +434,8 @@ qui-gon-jinn:
420434
height: '193'
421435
mass: '89'
422436
skin_color: fair
437+
vehicles:
438+
- tribubble-bongo
423439
created_at: '2014-12-19T16:54:53.618000Z'
424440
updated_at: '2014-12-20T21:17:50.375000Z'
425441

@@ -554,6 +570,8 @@ darth-maul:
554570
skin_color: red
555571
starships:
556572
- scimitar
573+
vehicles:
574+
- sith-speeder
557575
homeworld: dathomir
558576
created_at: '2014-12-19T18:00:41.929000Z'
559577
updated_at: '2014-12-20T21:17:50.403000Z'
@@ -821,6 +839,8 @@ dooku:
821839
height: '193'
822840
mass: '80'
823841
skin_color: fair
842+
vehicles:
843+
- flitknot-speeder
824844
homeworld: serenno
825845
created_at: '2014-12-20T16:52:14.726000Z'
826846
updated_at: '2014-12-20T21:17:50.462000Z'
@@ -858,6 +878,8 @@ zam-wesell:
858878
height: '168'
859879
mass: '55'
860880
skin_color: fair, green, yellow
881+
vehicles:
882+
- koro-2-exodrive-airspeeder
861883
homeworld: zolan
862884
created_at: '2014-12-20T16:57:44.471000Z'
863885
updated_at: '2014-12-20T21:17:50.468000Z'
@@ -973,6 +995,8 @@ grievous:
973995
skin_color: brown, white
974996
starships:
975997
- belbullab-22-starfighter
998+
vehicles:
999+
- tsmeu-6-personal-wheel-bike
9761000
homeworld: kalee
9771001
created_at: '2014-12-20T19:43:53.348000Z'
9781002
updated_at: '2014-12-20T21:17:50.488000Z'

test/fixtures/vehicles.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ snowspeeder:
6767
passengers: '0'
6868
cargo_capacity: '10'
6969
consumables: none
70+
pilots:
71+
- luke-skywalker
72+
- wedge-antilles
7073
created_at: '2014-12-15T12:22:12Z'
7174
updated_at: '2014-12-22T18:21:15.623033Z'
7275

@@ -108,6 +111,8 @@ at-st:
108111
passengers: '0'
109112
cargo_capacity: '200'
110113
consumables: none
114+
pilots:
115+
- chewbacca
111116
created_at: '2014-12-15T12:46:42.384000Z'
112117
updated_at: '2014-12-22T18:21:15.761584Z'
113118

@@ -182,6 +187,9 @@ imperial-speeder-bike:
182187
passengers: '1'
183188
cargo_capacity: '4'
184189
consumables: 1 day
190+
pilots:
191+
- luke-skywalker
192+
- leia-organa
185193
created_at: '2014-12-18T11:20:04.625000Z'
186194
updated_at: '2014-12-22T18:21:15.920537Z'
187195

@@ -265,6 +273,9 @@ tribubble-bongo:
265273
crew: '1'
266274
passengers: '2'
267275
cargo_capacity: '1600'
276+
pilots:
277+
- obi-wan-kenobi
278+
- qui-gon-jinn
268279
created_at: '2014-12-19T17:37:37.924000Z'
269280
updated_at: '2014-12-22T18:21:16.072083Z'
270281

@@ -279,6 +290,8 @@ sith-speeder:
279290
crew: '1'
280291
passengers: '0'
281292
cargo_capacity: '2'
293+
pilots:
294+
- darth-maul
282295
created_at: '2014-12-20T10:09:56.095000Z'
283296
updated_at: '2014-12-22T18:21:16.095041Z'
284297

@@ -294,6 +307,8 @@ zephyr-g-swoop-bike:
294307
passengers: '1'
295308
cargo_capacity: '200'
296309
consumables: none
310+
pilots:
311+
- anakin-skywalker
297312
created_at: '2014-12-20T16:24:16.026000Z'
298313
updated_at: '2014-12-22T18:21:16.117652Z'
299314

@@ -307,6 +322,8 @@ koro-2-exodrive-airspeeder:
307322
crew: '1'
308323
passengers: '1'
309324
cargo_capacity: '80'
325+
pilots:
326+
- zam-wesell
310327
created_at: '2014-12-20T17:17:33.526000Z'
311328
updated_at: '2014-12-22T18:21:16.140018Z'
312329

@@ -319,6 +336,8 @@ xj-6-airspeeder:
319336
max_atmosphering_speed: '720'
320337
crew: '1'
321338
passengers: '1'
339+
pilots:
340+
- anakin-skywalker
322341
created_at: '2014-12-20T17:19:19.991000Z'
323342
updated_at: '2014-12-22T18:21:16.150194Z'
324343

@@ -360,6 +379,8 @@ tsmeu-6-personal-wheel-bike:
360379
passengers: '1'
361380
cargo_capacity: '10'
362381
consumables: none
382+
pilots:
383+
- grievous
363384
created_at: '2014-12-20T19:43:54.870000Z'
364385
updated_at: '2014-12-22T18:21:16.422662Z'
365386

@@ -514,6 +535,8 @@ flitknot-speeder:
514535
max_atmosphering_speed: '634'
515536
crew: '1'
516537
passengers: '0'
538+
pilots:
539+
- dooku
517540
created_at: '2014-12-20T18:15:20.312000Z'
518541
updated_at: '2014-12-22T18:21:16.335005Z'
519542

0 commit comments

Comments
 (0)