質問編集履歴

2

書式の改善

2019/08/03 02:12

投稿

masaki5.5
masaki5.5

スコア10

test CHANGED
File without changes
test CHANGED
@@ -12,6 +12,58 @@
12
12
 
13
13
 
14
14
 
15
+ ```ruby
16
+
17
+ class User < ApplicationRecord
18
+
19
+ has_many :user_shop_items
20
+
21
+ has_many :user_shop_item_categories
22
+
23
+ has_many :shop_items, through: :user_shop_items
24
+
25
+ has_many :shop_item_categories, through: :user_shop_item_categories
26
+
27
+ end
28
+
29
+
30
+
31
+ class ShopItem < ApplicationRecord
32
+
33
+ has_many :user_shop_items
34
+
35
+ has_many :users, through: :user_shop_items
36
+
37
+ end
38
+
39
+
40
+
41
+ class UserShopItem < ApplicationRecord
42
+
43
+ belongs_to :user
44
+
45
+ belongs_to :shop_item
46
+
47
+ end
48
+
49
+
50
+
51
+ class UserShopItemCategory < ApplicationRecord
52
+
53
+ belongs_to :user
54
+
55
+ belongs_to :shop_item_category
56
+
57
+ end
58
+
59
+ ```
60
+
61
+
62
+
63
+ ---
64
+
65
+
66
+
15
67
  User
16
68
 
17
69
  |id|name||
@@ -90,60 +142,6 @@
90
142
 
91
143
 
92
144
 
93
- 各モデルの設定は以下のようになっています。
94
-
95
-
96
-
97
- ```ruby
98
-
99
- class User < ApplicationRecord
100
-
101
- has_many :user_shop_items
102
-
103
- has_many :user_shop_item_categories
104
-
105
- has_many :shop_items, through: :user_shop_items
106
-
107
- has_many :shop_item_categories, through: :user_shop_item_categories
108
-
109
- end
110
-
111
-
112
-
113
- class ShopItem < ApplicationRecord
114
-
115
- has_many :user_shop_items
116
-
117
- has_many :users, through: :user_shop_items
118
-
119
- end
120
-
121
-
122
-
123
- class UserShopItem < ApplicationRecord
124
-
125
- belongs_to :user
126
-
127
- belongs_to :shop_item
128
-
129
- end
130
-
131
-
132
-
133
- class UserShopItemCategory < ApplicationRecord
134
-
135
- belongs_to :user
136
-
137
- belongs_to :shop_item_category
138
-
139
- end
140
-
141
- ```
142
-
143
-
144
-
145
- ---
146
-
147
145
 
148
146
 
149
147
  2). UserShopItemテーブルにおいて、shop_item_idをもとにShopItemテーブルからshop_item_category_idを引っ張ってくる。

1

関連するモデルのコードの追記

2019/08/03 02:12

投稿

masaki5.5
masaki5.5

スコア10

test CHANGED
File without changes
test CHANGED
@@ -90,6 +90,62 @@
90
90
 
91
91
 
92
92
 
93
+ 各モデルの設定は以下のようになっています。
94
+
95
+
96
+
97
+ ```ruby
98
+
99
+ class User < ApplicationRecord
100
+
101
+ has_many :user_shop_items
102
+
103
+ has_many :user_shop_item_categories
104
+
105
+ has_many :shop_items, through: :user_shop_items
106
+
107
+ has_many :shop_item_categories, through: :user_shop_item_categories
108
+
109
+ end
110
+
111
+
112
+
113
+ class ShopItem < ApplicationRecord
114
+
115
+ has_many :user_shop_items
116
+
117
+ has_many :users, through: :user_shop_items
118
+
119
+ end
120
+
121
+
122
+
123
+ class UserShopItem < ApplicationRecord
124
+
125
+ belongs_to :user
126
+
127
+ belongs_to :shop_item
128
+
129
+ end
130
+
131
+
132
+
133
+ class UserShopItemCategory < ApplicationRecord
134
+
135
+ belongs_to :user
136
+
137
+ belongs_to :shop_item_category
138
+
139
+ end
140
+
141
+ ```
142
+
143
+
144
+
145
+ ---
146
+
147
+
148
+
93
149
  2). UserShopItemテーブルにおいて、shop_item_idをもとにShopItemテーブルからshop_item_category_idを引っ張ってくる。
94
150
 
95
151
  さらにそのshop_item_category_idとuser_idをもとに、UserShopItemCategoryテーブルからpriorityを引っ張ってくる。
@@ -134,12 +190,14 @@
134
190
 
135
191
  以上が私のやりたいことです。
136
192
 
137
- has_manyやbelongs_toも使ってみたのですが、なかなか上手くいきませんでした。
193
+
138
-
194
+
139
- とくに、UserShopItemとUserShopItemCategoryの関係が単純な親子にあたらないため、互い関連づけてvlookup関数のよに上手くデータを取っる方法がよくわかせんでした
195
+ UserShopItemとUserShopItemCategoryのテーブルwhereメソッドで繋ごとしのですが、そもそもの構築方法がよくわからずに苦戦してい
196
+
197
+
198
+
140
-
199
+ 質問が若干抽象的になってしまい、申し訳ございません。
141
-
142
-
200
+
143
- 解決ヒント教えていただけると、非常に助かります
201
+ さらに進展したらコードも追記いたしますが、構築方ヒントだけでも教えていただけると大変助かります
144
202
 
145
203
  どうぞよろしくお願いいたします。