質問編集履歴
1
テーブル追加等修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
ユーザー(出品者)が商品を出品し、別のユーザー(購入者)がそれを
|
1
|
+
ユーザー(出品者)がサービス(商品)を出品し、別のユーザー(購入者)がそれを購入できるというケースを想定した場合、以下の考え方で正しいでしょうか?誤りがあればご指摘頂けますと幸甚です。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
5
|
###前提
|
6
6
|
|
7
|
-
・ユーザー(出品者)と商品
|
7
|
+
・ユーザー(出品者)とサービス (商品): 1対多の関連付け
|
8
8
|
|
9
|
-
・商品とユーザー(購入者) : 多対多の関連付け
|
9
|
+
・サービス(商品)とユーザー(購入者) : 多対多の関連付け *1つのサービス(商品)に複数のユーザー(購入者)が購入できる前提。
|
10
10
|
|
11
|
-
・ユーザーは出品者にも購入者にもなれる : 出品者と購入者は同じ`model`
|
11
|
+
・ユーザーは出品者にも購入者にもなれる : 出品者と購入者は同じ`model`
|
12
12
|
|
13
13
|
|
14
14
|
|
@@ -20,7 +20,99 @@
|
|
20
20
|
|
21
21
|
・すなわち、以下のイメージ
|
22
22
|
|
23
|
-
`user` <-> `product` <-> `entry` <-> `user`
|
23
|
+
`user` (1)<-> (多)`product`(1) <-> (多)`entry`(多) <->(1) `user`
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
###各モデルのテーブル(migration file) *単純化してます
|
28
|
+
|
29
|
+
**User**
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
|
33
|
+
#User
|
34
|
+
|
35
|
+
class CreateUsers < ActiveRecord::Migration[5.0]
|
36
|
+
|
37
|
+
def change
|
38
|
+
|
39
|
+
create_table :users do |t|
|
40
|
+
|
41
|
+
t.string :email, null: false # メールアドレス
|
42
|
+
|
43
|
+
t.string :family_name # 姓
|
44
|
+
|
45
|
+
t.string :given_name # 名
|
46
|
+
|
47
|
+
t.string :address # 住所
|
48
|
+
|
49
|
+
t.text :user_profile # プロフィール
|
50
|
+
|
51
|
+
t.string :hashed_password # パスワード
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
t.timestamps
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
#Product
|
66
|
+
|
67
|
+
class CreateProducts < ActiveRecord::Migration[5.0]
|
68
|
+
|
69
|
+
def change
|
70
|
+
|
71
|
+
create_table :products do |t|
|
72
|
+
|
73
|
+
t.references :user #外部キー
|
74
|
+
|
75
|
+
t.integer :price #価格
|
76
|
+
|
77
|
+
t.string :title #タイトル
|
78
|
+
|
79
|
+
t.datetime :posted_at #商品投稿日
|
80
|
+
|
81
|
+
t.binary :data #写真
|
82
|
+
|
83
|
+
t.timestamps null: false
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
#Entry
|
94
|
+
|
95
|
+
class CreateEntries < ActiveRecord::Migration[5.0]
|
96
|
+
|
97
|
+
def change
|
98
|
+
|
99
|
+
create_table :entries do |t|
|
100
|
+
|
101
|
+
t.references :product, null: false
|
102
|
+
|
103
|
+
t.references :user, null: false
|
104
|
+
|
105
|
+
t.timestamps
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
```
|
24
116
|
|
25
117
|
|
26
118
|
|
@@ -60,9 +152,9 @@
|
|
60
152
|
|
61
153
|
class Entry < ApplicationRecord
|
62
154
|
|
63
|
-
|
155
|
+
belongs_to :applied_products, class_name: "Product", foreign_key: "product_id"
|
64
156
|
|
65
|
-
|
157
|
+
belongs_to :applicants, class_name: "User", foreign_key: "user_id"
|
66
158
|
|
67
159
|
end
|
68
160
|
|