Rails 5.0: Add self join table that references existing table
We're creating a flight scheduling program that schedules employees and planes for flights.
Here are the models that currently exist
Airport
Pilot
FlightAttendant
Aircraft
We are starting with only four airports. We want to fill in the four airports in the Airports table as soon as the app launches, and then make a self-join table from the Airports that lists all possible origin-destination combinations and their duration.
How would we do that?
I've seen some stuff online about it but it looks like its done when we create the models, but our models are already made, so I can't figure out the migration and how to fill in that table automatically. We know the durations and just need to feed them in.
EDIT:
In response to the flight times: we plan to store them as an integer which is the number of minutes it takes to complete a flight. The airports are in Midwestern cities chosen at random in the Central Time Zone.
Lincoln and Iowa City: 32 mins
Lincoln and Evanston: 57 mins
Lincoln and West Lafayette: 62 mins
Iowa City and Evanston: 24 mins
Iowa City and West Lafayette: 31 mins
Evanston and West Lafayette: 13 mins
For further detail
This is the specific migration which created the Airports table
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
end
end
full_name is simply the name like Evanston. flight_code is the the three letter code to represent it, like EVA.
The model is currently empty. Do I need to add something in it first before I add the association columns, or do I need to generate the migration to create the join table and then alter the Airport model?
ruby-on-rails migration self-join
add a comment |
We're creating a flight scheduling program that schedules employees and planes for flights.
Here are the models that currently exist
Airport
Pilot
FlightAttendant
Aircraft
We are starting with only four airports. We want to fill in the four airports in the Airports table as soon as the app launches, and then make a self-join table from the Airports that lists all possible origin-destination combinations and their duration.
How would we do that?
I've seen some stuff online about it but it looks like its done when we create the models, but our models are already made, so I can't figure out the migration and how to fill in that table automatically. We know the durations and just need to feed them in.
EDIT:
In response to the flight times: we plan to store them as an integer which is the number of minutes it takes to complete a flight. The airports are in Midwestern cities chosen at random in the Central Time Zone.
Lincoln and Iowa City: 32 mins
Lincoln and Evanston: 57 mins
Lincoln and West Lafayette: 62 mins
Iowa City and Evanston: 24 mins
Iowa City and West Lafayette: 31 mins
Evanston and West Lafayette: 13 mins
For further detail
This is the specific migration which created the Airports table
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
end
end
full_name is simply the name like Evanston. flight_code is the the three letter code to represent it, like EVA.
The model is currently empty. Do I need to add something in it first before I add the association columns, or do I need to generate the migration to create the join table and then alter the Airport model?
ruby-on-rails migration self-join
1
"We know the durations and just need to feed them in." Please provide details about how do you want to store that durations, in which table?
– Ilya Konyukhov
Nov 14 '18 at 20:50
Re "How would we do that?": What exactly is "that"? Why do you have to do anything? Are you talking about displaying something to the user? Materializing a table/database per-user for optimization? Please be clearer about exactly the specific task you are having problems with is & what you are already able to do. Minimal, Complete, and Verifiable example please--cut & paste & runnable text with input & desired output & specification. PS Please do not append EDITs/UPDATEs, edit to the best presentation. Adding more does not make an original clear.
– philipxy
Nov 15 '18 at 1:27
add a comment |
We're creating a flight scheduling program that schedules employees and planes for flights.
Here are the models that currently exist
Airport
Pilot
FlightAttendant
Aircraft
We are starting with only four airports. We want to fill in the four airports in the Airports table as soon as the app launches, and then make a self-join table from the Airports that lists all possible origin-destination combinations and their duration.
How would we do that?
I've seen some stuff online about it but it looks like its done when we create the models, but our models are already made, so I can't figure out the migration and how to fill in that table automatically. We know the durations and just need to feed them in.
EDIT:
In response to the flight times: we plan to store them as an integer which is the number of minutes it takes to complete a flight. The airports are in Midwestern cities chosen at random in the Central Time Zone.
Lincoln and Iowa City: 32 mins
Lincoln and Evanston: 57 mins
Lincoln and West Lafayette: 62 mins
Iowa City and Evanston: 24 mins
Iowa City and West Lafayette: 31 mins
Evanston and West Lafayette: 13 mins
For further detail
This is the specific migration which created the Airports table
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
end
end
full_name is simply the name like Evanston. flight_code is the the three letter code to represent it, like EVA.
The model is currently empty. Do I need to add something in it first before I add the association columns, or do I need to generate the migration to create the join table and then alter the Airport model?
ruby-on-rails migration self-join
We're creating a flight scheduling program that schedules employees and planes for flights.
Here are the models that currently exist
Airport
Pilot
FlightAttendant
Aircraft
We are starting with only four airports. We want to fill in the four airports in the Airports table as soon as the app launches, and then make a self-join table from the Airports that lists all possible origin-destination combinations and their duration.
How would we do that?
I've seen some stuff online about it but it looks like its done when we create the models, but our models are already made, so I can't figure out the migration and how to fill in that table automatically. We know the durations and just need to feed them in.
EDIT:
In response to the flight times: we plan to store them as an integer which is the number of minutes it takes to complete a flight. The airports are in Midwestern cities chosen at random in the Central Time Zone.
Lincoln and Iowa City: 32 mins
Lincoln and Evanston: 57 mins
Lincoln and West Lafayette: 62 mins
Iowa City and Evanston: 24 mins
Iowa City and West Lafayette: 31 mins
Evanston and West Lafayette: 13 mins
For further detail
This is the specific migration which created the Airports table
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
end
end
full_name is simply the name like Evanston. flight_code is the the three letter code to represent it, like EVA.
The model is currently empty. Do I need to add something in it first before I add the association columns, or do I need to generate the migration to create the join table and then alter the Airport model?
ruby-on-rails migration self-join
ruby-on-rails migration self-join
edited Nov 15 '18 at 1:16
ABvsPred
asked Nov 14 '18 at 20:08
ABvsPredABvsPred
375
375
1
"We know the durations and just need to feed them in." Please provide details about how do you want to store that durations, in which table?
– Ilya Konyukhov
Nov 14 '18 at 20:50
Re "How would we do that?": What exactly is "that"? Why do you have to do anything? Are you talking about displaying something to the user? Materializing a table/database per-user for optimization? Please be clearer about exactly the specific task you are having problems with is & what you are already able to do. Minimal, Complete, and Verifiable example please--cut & paste & runnable text with input & desired output & specification. PS Please do not append EDITs/UPDATEs, edit to the best presentation. Adding more does not make an original clear.
– philipxy
Nov 15 '18 at 1:27
add a comment |
1
"We know the durations and just need to feed them in." Please provide details about how do you want to store that durations, in which table?
– Ilya Konyukhov
Nov 14 '18 at 20:50
Re "How would we do that?": What exactly is "that"? Why do you have to do anything? Are you talking about displaying something to the user? Materializing a table/database per-user for optimization? Please be clearer about exactly the specific task you are having problems with is & what you are already able to do. Minimal, Complete, and Verifiable example please--cut & paste & runnable text with input & desired output & specification. PS Please do not append EDITs/UPDATEs, edit to the best presentation. Adding more does not make an original clear.
– philipxy
Nov 15 '18 at 1:27
1
1
"We know the durations and just need to feed them in." Please provide details about how do you want to store that durations, in which table?
– Ilya Konyukhov
Nov 14 '18 at 20:50
"We know the durations and just need to feed them in." Please provide details about how do you want to store that durations, in which table?
– Ilya Konyukhov
Nov 14 '18 at 20:50
Re "How would we do that?": What exactly is "that"? Why do you have to do anything? Are you talking about displaying something to the user? Materializing a table/database per-user for optimization? Please be clearer about exactly the specific task you are having problems with is & what you are already able to do. Minimal, Complete, and Verifiable example please--cut & paste & runnable text with input & desired output & specification. PS Please do not append EDITs/UPDATEs, edit to the best presentation. Adding more does not make an original clear.
– philipxy
Nov 15 '18 at 1:27
Re "How would we do that?": What exactly is "that"? Why do you have to do anything? Are you talking about displaying something to the user? Materializing a table/database per-user for optimization? Please be clearer about exactly the specific task you are having problems with is & what you are already able to do. Minimal, Complete, and Verifiable example please--cut & paste & runnable text with input & desired output & specification. PS Please do not append EDITs/UPDATEs, edit to the best presentation. Adding more does not make an original clear.
– philipxy
Nov 15 '18 at 1:27
add a comment |
1 Answer
1
active
oldest
votes
You can run ruby code after the migration, Rails code in this case:
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
Airport.create(params_for_airport1)
Airport.create(params_for_airport2)
Airport.create(params_for_airport3)
Airport.create(params_for_airport4)
end
end
Then, for the durations table, I imagine you'll have something like a "FlightDuration" using a table with 3 columns: airport_from_id
, airport_to_id
, duration
. You can do the same as before, you create the table and right after the create_table
just create the objects with the data.
You'll have to add the relationships to Airport and to FlightDuration, something like:
class Airport < ApplicationRecord
has_many :flight_durations_from, class_name: 'FlightDuration', inverse_of: :airport_from
has_many :flight_durations_to, class_name: 'FlightDuration', inverse_of: :airport_to
end
class FlightDuration < ApplicationRecord
belongs_to :airport_from, class_name: 'Airport', foreign_key: :airport_from_id
belongs_to :airport_to, class_name: 'Airport', foreign_key: :airport_to_id
end
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308019%2frails-5-0-add-self-join-table-that-references-existing-table%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can run ruby code after the migration, Rails code in this case:
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
Airport.create(params_for_airport1)
Airport.create(params_for_airport2)
Airport.create(params_for_airport3)
Airport.create(params_for_airport4)
end
end
Then, for the durations table, I imagine you'll have something like a "FlightDuration" using a table with 3 columns: airport_from_id
, airport_to_id
, duration
. You can do the same as before, you create the table and right after the create_table
just create the objects with the data.
You'll have to add the relationships to Airport and to FlightDuration, something like:
class Airport < ApplicationRecord
has_many :flight_durations_from, class_name: 'FlightDuration', inverse_of: :airport_from
has_many :flight_durations_to, class_name: 'FlightDuration', inverse_of: :airport_to
end
class FlightDuration < ApplicationRecord
belongs_to :airport_from, class_name: 'Airport', foreign_key: :airport_from_id
belongs_to :airport_to, class_name: 'Airport', foreign_key: :airport_to_id
end
add a comment |
You can run ruby code after the migration, Rails code in this case:
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
Airport.create(params_for_airport1)
Airport.create(params_for_airport2)
Airport.create(params_for_airport3)
Airport.create(params_for_airport4)
end
end
Then, for the durations table, I imagine you'll have something like a "FlightDuration" using a table with 3 columns: airport_from_id
, airport_to_id
, duration
. You can do the same as before, you create the table and right after the create_table
just create the objects with the data.
You'll have to add the relationships to Airport and to FlightDuration, something like:
class Airport < ApplicationRecord
has_many :flight_durations_from, class_name: 'FlightDuration', inverse_of: :airport_from
has_many :flight_durations_to, class_name: 'FlightDuration', inverse_of: :airport_to
end
class FlightDuration < ApplicationRecord
belongs_to :airport_from, class_name: 'Airport', foreign_key: :airport_from_id
belongs_to :airport_to, class_name: 'Airport', foreign_key: :airport_to_id
end
add a comment |
You can run ruby code after the migration, Rails code in this case:
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
Airport.create(params_for_airport1)
Airport.create(params_for_airport2)
Airport.create(params_for_airport3)
Airport.create(params_for_airport4)
end
end
Then, for the durations table, I imagine you'll have something like a "FlightDuration" using a table with 3 columns: airport_from_id
, airport_to_id
, duration
. You can do the same as before, you create the table and right after the create_table
just create the objects with the data.
You'll have to add the relationships to Airport and to FlightDuration, something like:
class Airport < ApplicationRecord
has_many :flight_durations_from, class_name: 'FlightDuration', inverse_of: :airport_from
has_many :flight_durations_to, class_name: 'FlightDuration', inverse_of: :airport_to
end
class FlightDuration < ApplicationRecord
belongs_to :airport_from, class_name: 'Airport', foreign_key: :airport_from_id
belongs_to :airport_to, class_name: 'Airport', foreign_key: :airport_to_id
end
You can run ruby code after the migration, Rails code in this case:
class CreateAirports < ActiveRecord::Migration[5.2]
def change
create_table :airports do |t|
t.string :full_name
t.string :flight_code
t.timestamps
end
Airport.create(params_for_airport1)
Airport.create(params_for_airport2)
Airport.create(params_for_airport3)
Airport.create(params_for_airport4)
end
end
Then, for the durations table, I imagine you'll have something like a "FlightDuration" using a table with 3 columns: airport_from_id
, airport_to_id
, duration
. You can do the same as before, you create the table and right after the create_table
just create the objects with the data.
You'll have to add the relationships to Airport and to FlightDuration, something like:
class Airport < ApplicationRecord
has_many :flight_durations_from, class_name: 'FlightDuration', inverse_of: :airport_from
has_many :flight_durations_to, class_name: 'FlightDuration', inverse_of: :airport_to
end
class FlightDuration < ApplicationRecord
belongs_to :airport_from, class_name: 'Airport', foreign_key: :airport_from_id
belongs_to :airport_to, class_name: 'Airport', foreign_key: :airport_to_id
end
answered Nov 15 '18 at 1:54
arieljuodarieljuod
7,23711221
7,23711221
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308019%2frails-5-0-add-self-join-table-that-references-existing-table%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"We know the durations and just need to feed them in." Please provide details about how do you want to store that durations, in which table?
– Ilya Konyukhov
Nov 14 '18 at 20:50
Re "How would we do that?": What exactly is "that"? Why do you have to do anything? Are you talking about displaying something to the user? Materializing a table/database per-user for optimization? Please be clearer about exactly the specific task you are having problems with is & what you are already able to do. Minimal, Complete, and Verifiable example please--cut & paste & runnable text with input & desired output & specification. PS Please do not append EDITs/UPDATEs, edit to the best presentation. Adding more does not make an original clear.
– philipxy
Nov 15 '18 at 1:27