質問編集履歴
2
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -36,4 +36,44 @@
|
|
36
36
|
project.owner
|
37
37
|
project.followers
|
38
38
|
|
39
|
+
```
|
40
|
+
|
41
|
+
追記:
|
42
|
+
shishou == followerと変えました.
|
43
|
+
|
44
|
+
以下のように書き,
|
45
|
+
project.owner
|
46
|
+
project.shishous
|
47
|
+
user.project_as_owner
|
48
|
+
|
49
|
+
はコンソールでセット,取得できるようになったのですが,
|
50
|
+
project.shishous << user1
|
51
|
+
と入れた時,
|
52
|
+
user1.projects_as_shishou が表示されないようです.
|
53
|
+
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class Project < ApplicationRecord
|
57
|
+
|
58
|
+
has_many :project_shishous
|
59
|
+
has_many :shishous, through: :project_shishous, source: 'shishou'
|
60
|
+
|
61
|
+
belongs_to :owner, class_name: "User", foreign_key: "owner_id"
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
class User < ApplicationRecord
|
66
|
+
has_many :project_shishous
|
67
|
+
has_many :projects_as_shishou, through: :project_shishous, source: 'project'
|
68
|
+
|
69
|
+
has_many :projects_as_owner, class_name: 'Project', foreign_key: 'owner_id'
|
70
|
+
end
|
71
|
+
|
72
|
+
class ProjectShishou < ApplicationRecord
|
73
|
+
belongs_to :shishou, class_name: 'User', foreign_key: 'shishou_id'
|
74
|
+
belongs_to :project
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
|
39
79
|
```
|
1
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,25 +1,38 @@
|
|
1
1
|
Rails で,モデルの設定に悩んでいます.
|
2
2
|
以下のようなことを実現したいのですが,うまくいかないため,Model, Migrationファイルに何を書けばいいのか教えていただきたいです.
|
3
3
|
|
4
|
+
以下のモデルが実現したいものです.
|
5
|
+
|
4
6
|
```ruby
|
5
7
|
|
6
|
-
#
|
8
|
+
# model
|
9
|
+
# 中間テーブル
|
10
|
+
class ProjectUser < ApplicationRecord
|
7
|
-
|
11
|
+
belongs_to :user
|
8
|
-
has_many :projects, # as owner
|
9
|
-
belongs_to :project
|
12
|
+
belongs_to :project
|
10
13
|
end
|
11
14
|
|
15
|
+
class User < ApplicationRecord
|
16
|
+
|
12
|
-
|
17
|
+
has_many :project_users
|
13
|
-
belongs_to :owner, class_name: 'User'
|
14
|
-
has_many :
|
18
|
+
# has_many :projects, through: :project_users
|
19
|
+
has_many :project_as_owner
|
20
|
+
has_many :project_as_follwer
|
15
21
|
end
|
16
|
-
|
17
22
|
|
23
|
+
class Project < ApplicationRecord
|
24
|
+
has_many :project_users
|
25
|
+
# has_many :users, through: :project_users
|
26
|
+
has_many :follower, class_name: 'User'
|
27
|
+
has_one :owner, class_name: 'User'
|
28
|
+
end
|
29
|
+
|
18
30
|
```
|
19
31
|
以下のように値を取得したいです.
|
20
32
|
```
|
21
33
|
# result
|
22
|
-
user.
|
34
|
+
user.projects_as_owner
|
35
|
+
user.project_as_follower
|
23
36
|
project.owner
|
24
37
|
project.followers
|
25
38
|
|