質問編集履歴

1

modelの追加

2020/06/14 00:53

投稿

BKBKB
BKBKB

スコア8

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- コントローラーのindexアクションで、指定のuser_idのデータを持ったComapnyオブジェクトを作りたいのですが、どのようにすればよいでしょうか。
7
+ コントローラーのindexアクションで、**current_user**のデータを持ったComapnyオブジェクトを作りたいのですが、どのようにすればよいでしょうか。
8
8
 
9
9
 
10
10
 
@@ -66,6 +66,84 @@
66
66
 
67
67
  ```
68
68
 
69
+ user.rb
70
+
71
+ ```
72
+
73
+ class User < ApplicationRecord
74
+
75
+ devise :database_authenticatable, :registerable,
76
+
77
+ :recoverable, :rememberable, :validatable
78
+
79
+
80
+
81
+ has_many :documents
82
+
83
+ has_many :documents, foreign_key: 'user_id'
84
+
85
+ has_many :companies, through: :documents
86
+
87
+ has_many :items, through: :documents
88
+
89
+
90
+
91
+ end
92
+
93
+ ```
94
+
95
+ company.rb
96
+
97
+ ```
98
+
99
+ class Company < ApplicationRecord
100
+
101
+ validates :name, presence: true
102
+
103
+ has_many :documents
104
+
105
+ has_many :documents, foreign_key: 'company_id'
106
+
107
+ has_many :users, through: :documents
108
+
109
+ end
110
+
111
+ ```
112
+
113
+ item.rb
114
+
115
+ ```
116
+
117
+ class Item < ApplicationRecord
118
+
119
+ has_many :documents
120
+
121
+ has_many :documents, foreign_key: 'item_id'
122
+
123
+ has_many :users, through: :documents
124
+
125
+ end
126
+
127
+ ```
128
+
129
+ document.rb
130
+
131
+ ```
132
+
133
+ class Document < ApplicationRecord
134
+
135
+ belongs_to :company
136
+
137
+ belongs_to :item
138
+
139
+ belongs_to :user
140
+
141
+ end
142
+
143
+ ```
144
+
145
+
146
+
69
147
  Ruby 2.5.1
70
148
 
71
149
  Rail 5.0.7.2