Last active
June 20, 2020 22:55
-
-
Save luchiago/a306526e56a26f2d260b2f68701c2667 to your computer and use it in GitHub Desktop.
Migration for multiple foreign keys with the same model in Rails 6
This file contains hidden or 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
class CreateMeetings < ActiveRecord::Migration[6.0] | |
def change | |
create_table :meetings do |t| | |
t.datetime :starts_at, null: false | |
t.datetime :ends_at, null: false | |
t.references :available_user, null: false | |
t.references :requester_user, null: false | |
t.timestamps | |
end | |
end | |
end |
This file contains hidden or 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
class Meeting < ApplicationRecord | |
belongs_to :available_user, class_name: 'User' | |
belongs_to :requester_user, class_name: 'User' | |
end |
This file contains hidden or 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
class User < ApplicationRecord | |
has_many :available_user_meetings, class_name: 'Meeting', foreign_key: 'available_user_id' | |
has_many :requester_user_meetings, class_name: 'Meeting', foreign_key: 'requester_user_id' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Posts about this gist:
https://dev.to/luchiago/multiple-foreign-keys-for-the-same-model-in-rails-6-7ml
https://medium.com/@lucashiago63/multiple-foreign-keys-for-the-same-model-in-rails-6-18ac4b7a7c6a