-
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add models for UserLanguages backfill values
- Loading branch information
1 parent
88cb640
commit 43bc4e9
Showing
12 changed files
with
125 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class UserLanguage < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :language | ||
|
||
validates :language, uniqueness: {scope: :user} | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: user_languages | ||
# | ||
# id :bigint not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# language_id :bigint | ||
# user_id :bigint | ||
# | ||
# Indexes | ||
# | ||
# index_user_languages_on_language_id (language_id) | ||
# index_user_languages_on_language_id_and_user_id (language_id,user_id) UNIQUE | ||
# index_user_languages_on_user_id (user_id) | ||
# |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class AddUserLanguages < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
|
||
def change | ||
create_table :user_languages do |t| | ||
t.references :user | ||
t.references :language | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :user_languages, [:language_id, :user_id], unique: true, algorithm: :concurrently | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
db/migrate/20221012203806_populate_user_languages_from_languages_users.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class PopulateUserLanguagesFromLanguagesUsers < ActiveRecord::Migration[7.0] | ||
def change | ||
query = Arel.sql("select language_id, user_id from languages_users") | ||
old_join_table_entries = ActiveRecord::Base.connection.execute(query).to_a | ||
|
||
old_join_table_entries.each do |entry| | ||
UserLanguage.create(user_id: entry["user_id"], language_id: entry["language_id"]) | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :user_language do | ||
user | ||
language | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe UserLanguage, type: :model do | ||
it { is_expected.to belong_to(:language) } | ||
it { is_expected.to belong_to(:user) } | ||
|
||
it "validates uniqueness of language scoped to user" do | ||
existing_record = create(:user_language) | ||
new_record = build(:user_language, user: existing_record.user, language: existing_record.language) | ||
expect(new_record).not_to be_valid | ||
end | ||
end |
This file contains 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
This file contains 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