teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

書式の改善

2019/08/03 02:12

投稿

masaki5.5
masaki5.5

スコア10

title CHANGED
File without changes
body CHANGED
@@ -5,6 +5,32 @@
5
5
 
6
6
  1). モデル(テーブル)が以下のように4つあります。
7
7
 
8
+ ```ruby
9
+ class User < ApplicationRecord
10
+ has_many :user_shop_items
11
+ has_many :user_shop_item_categories
12
+ has_many :shop_items, through: :user_shop_items
13
+ has_many :shop_item_categories, through: :user_shop_item_categories
14
+ end
15
+
16
+ class ShopItem < ApplicationRecord
17
+ has_many :user_shop_items
18
+ has_many :users, through: :user_shop_items
19
+ end
20
+
21
+ class UserShopItem < ApplicationRecord
22
+ belongs_to :user
23
+ belongs_to :shop_item
24
+ end
25
+
26
+ class UserShopItemCategory < ApplicationRecord
27
+ belongs_to :user
28
+ belongs_to :shop_item_category
29
+ end
30
+ ```
31
+
32
+ ---
33
+
8
34
  User
9
35
  |id|name||
10
36
  |:--:|:--:|--:|
@@ -44,34 +70,7 @@
44
70
 
45
71
  ---
46
72
 
47
- 各モデルの設定は以下のようになっています。
48
73
 
49
- ```ruby
50
- class User < ApplicationRecord
51
- has_many :user_shop_items
52
- has_many :user_shop_item_categories
53
- has_many :shop_items, through: :user_shop_items
54
- has_many :shop_item_categories, through: :user_shop_item_categories
55
- end
56
-
57
- class ShopItem < ApplicationRecord
58
- has_many :user_shop_items
59
- has_many :users, through: :user_shop_items
60
- end
61
-
62
- class UserShopItem < ApplicationRecord
63
- belongs_to :user
64
- belongs_to :shop_item
65
- end
66
-
67
- class UserShopItemCategory < ApplicationRecord
68
- belongs_to :user
69
- belongs_to :shop_item_category
70
- end
71
- ```
72
-
73
- ---
74
-
75
74
  2). UserShopItemテーブルにおいて、shop_item_idをもとにShopItemテーブルからshop_item_category_idを引っ張ってくる。
76
75
  さらにそのshop_item_category_idとuser_idをもとに、UserShopItemCategoryテーブルからpriorityを引っ張ってくる。
77
76
  最後に、shop_item_idをもとに、ShopItemテーブルからshop_item_nameを引っ張ってくる。

1

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

2019/08/03 02:12

投稿

masaki5.5
masaki5.5

スコア10

title CHANGED
File without changes
body CHANGED
@@ -44,6 +44,34 @@
44
44
 
45
45
  ---
46
46
 
47
+ 各モデルの設定は以下のようになっています。
48
+
49
+ ```ruby
50
+ class User < ApplicationRecord
51
+ has_many :user_shop_items
52
+ has_many :user_shop_item_categories
53
+ has_many :shop_items, through: :user_shop_items
54
+ has_many :shop_item_categories, through: :user_shop_item_categories
55
+ end
56
+
57
+ class ShopItem < ApplicationRecord
58
+ has_many :user_shop_items
59
+ has_many :users, through: :user_shop_items
60
+ end
61
+
62
+ class UserShopItem < ApplicationRecord
63
+ belongs_to :user
64
+ belongs_to :shop_item
65
+ end
66
+
67
+ class UserShopItemCategory < ApplicationRecord
68
+ belongs_to :user
69
+ belongs_to :shop_item_category
70
+ end
71
+ ```
72
+
73
+ ---
74
+
47
75
  2). UserShopItemテーブルにおいて、shop_item_idをもとにShopItemテーブルからshop_item_category_idを引っ張ってくる。
48
76
  さらにそのshop_item_category_idとuser_idをもとに、UserShopItemCategoryテーブルからpriorityを引っ張ってくる。
49
77
  最後に、shop_item_idをもとに、ShopItemテーブルからshop_item_nameを引っ張ってくる。
@@ -66,8 +94,9 @@
66
94
  D'
67
95
 
68
96
  以上が私のやりたいことです。
69
- has_manyやbelongs_toも使ってみたのですが、なかなか上手くいきませんでした。
70
- とくに、UserShopItemとUserShopItemCategoryの関係が単純な親子にあたらないため、互いを関連づけてvlookup関数のように上手くデータを取ってくる方法がよくわかりませんでした。
71
97
 
98
+ UserShopItemとUserShopItemCategoryのテーブルをwhereメソッドで繋ごうとしているのですが、そもそもの構築方法がよくわからずに苦戦しています。
99
+
100
+ 質問が若干抽象的になってしまい、申し訳ございません。
72
- 解決ヒント教えていただけると、非常に助かります
101
+ さらに進展したらコードも追記いたしますが、構築方ヒントだけでも教えていただけると大変助かります
73
102
  どうぞよろしくお願いいたします。