Rails 5.0: Add self join table that references existing table










-1















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?










share|improve this question



















  • 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















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?










share|improve this question



















  • 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








-1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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













1 Answer
1






active

oldest

votes


















0














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





share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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





    share|improve this answer



























      0














      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





      share|improve this answer

























        0












        0








        0







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 1:54









        arieljuodarieljuod

        7,23711221




        7,23711221





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto