Parcourir la source

ajout d'une table de jointure entre les modèles User et Team

Lou il y a 3 ans
Parent
révision
ec063ca908

+ 4
- 0
app/models/joined_team_user.rb Voir le fichier

@@ -0,0 +1,4 @@
1
+class JoinedTeamUser < ApplicationRecord
2
+  belongs_to :user
3
+  belongs_to :team
4
+end

+ 2
- 0
app/models/team.rb Voir le fichier

@@ -1,4 +1,6 @@
1 1
 class Team < ApplicationRecord
2 2
     validates :name, uniqueness: true, presence: true
3 3
 
4
+    has_many :joined_team_users
5
+    has_many :users, through: :joined_team_users
4 6
 end

+ 4
- 1
app/models/user.rb Voir le fichier

@@ -10,6 +10,9 @@ class User < ApplicationRecord
10 10
   has_many :activities, :through => :joined_user_activities
11 11
 
12 12
   has_many :tasks
13
-
13
+  
14
+  has_many :joined_team_users
15
+  has_many :teams, through: :joined_team_users
16
+  
14 17
   has_secure_password
15 18
 end

+ 0
- 8
db/migrate/20210803145424_create_teams_users_join_table.rb Voir le fichier

@@ -1,8 +0,0 @@
1
-class CreateTeamsUsersJoinTable < ActiveRecord::Migration[6.1]
2
-  def change
3
-    create_join_table :teams, :users do |t|
4
-      t.index :team_id
5
-      t.index :user_id
6
-    end
7
-  end
8
-end

+ 10
- 0
db/migrate/20210803152520_create_joined_team_users.rb Voir le fichier

@@ -0,0 +1,10 @@
1
+class CreateJoinedTeamUsers < ActiveRecord::Migration[6.1]
2
+  def change
3
+    create_table :joined_team_users do |t|
4
+      t.references :user
5
+      t.references :team
6
+
7
+      t.timestamps
8
+    end
9
+  end
10
+end

+ 10
- 8
db/schema.rb Voir le fichier

@@ -10,7 +10,7 @@
10 10
 #
11 11
 # It's strongly recommended that you check this file into your version control system.
12 12
 
13
-ActiveRecord::Schema.define(version: 2021_08_03_145424) do
13
+ActiveRecord::Schema.define(version: 2021_08_03_152520) do
14 14
 
15 15
   # These are extensions that must be enabled in order to support this database
16 16
   enable_extension "plpgsql"
@@ -26,6 +26,15 @@ ActiveRecord::Schema.define(version: 2021_08_03_145424) do
26 26
     t.index ["name"], name: "index_activities_on_name"
27 27
   end
28 28
 
29
+  create_table "joined_team_users", force: :cascade do |t|
30
+    t.bigint "user_id"
31
+    t.bigint "team_id"
32
+    t.datetime "created_at", precision: 6, null: false
33
+    t.datetime "updated_at", precision: 6, null: false
34
+    t.index ["team_id"], name: "index_joined_team_users_on_team_id"
35
+    t.index ["user_id"], name: "index_joined_team_users_on_user_id"
36
+  end
37
+
29 38
   create_table "joined_user_activities", force: :cascade do |t|
30 39
     t.bigint "user_id"
31 40
     t.bigint "activity_id"
@@ -56,13 +65,6 @@ ActiveRecord::Schema.define(version: 2021_08_03_145424) do
56 65
     t.index ["name"], name: "index_teams_on_name", unique: true
57 66
   end
58 67
 
59
-  create_table "teams_users", id: false, force: :cascade do |t|
60
-    t.bigint "team_id", null: false
61
-    t.bigint "user_id", null: false
62
-    t.index ["team_id"], name: "index_teams_users_on_team_id"
63
-    t.index ["user_id"], name: "index_teams_users_on_user_id"
64
-  end
65
-
66 68
   create_table "users", force: :cascade do |t|
67 69
     t.string "email", null: false
68 70
     t.string "username", null: false

BIN
erd.pdf Voir le fichier


+ 11
- 0
test/fixtures/joined_team_users.yml Voir le fichier

@@ -0,0 +1,11 @@
1
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+# This model initially had no columns defined. If you add columns to the
4
+# model remove the '{}' from the fixture names and add the columns immediately
5
+# below each fixture, per the syntax in the comments below
6
+#
7
+one: {}
8
+# column: value
9
+#
10
+two: {}
11
+# column: value

+ 7
- 0
test/models/joined_team_user_test.rb Voir le fichier

@@ -0,0 +1,7 @@
1
+require "test_helper"
2
+
3
+class JoinedTeamUserTest < ActiveSupport::TestCase
4
+  # test "the truth" do
5
+  #   assert true
6
+  # end
7
+end

Loading…
Annuler
Enregistrer