質問編集履歴
1
参考データ添付しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,3 +29,217 @@
|
|
29
29
|
```
|
30
30
|
|
31
31
|
でviewにeach使用してproduct.total_countで各productがどれだけ売れたかはわかるのですが、ここに.order.limitとかで実装できると思ったのですがうまくいきませんでした。何かわかりそうな方いましたらとよろしくお願いいたします!
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
☆追記
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
上記total_countはメソッドです。なのでメソッドで引き出した値を並び替えたいという事です。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
以下参考データ添付させていただきます。
|
44
|
+
|
45
|
+
schema.rb
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
ActiveRecord::Schema.define(version: 20180508113923) do
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
create_table "carts", force: :cascade do |t|
|
56
|
+
|
57
|
+
t.datetime "created_at", null: false
|
58
|
+
|
59
|
+
t.datetime "updated_at", null: false
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
create_table "line_items", force: :cascade do |t|
|
66
|
+
|
67
|
+
t.integer "product_id"
|
68
|
+
|
69
|
+
t.integer "cart_id"
|
70
|
+
|
71
|
+
t.datetime "created_at", null: false
|
72
|
+
|
73
|
+
t.datetime "updated_at", null: false
|
74
|
+
|
75
|
+
t.integer "quantity", default: 1
|
76
|
+
|
77
|
+
t.integer "order_id"
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
create_table "orders", force: :cascade do |t|
|
84
|
+
|
85
|
+
t.string "name"
|
86
|
+
|
87
|
+
t.text "address"
|
88
|
+
|
89
|
+
t.string "email"
|
90
|
+
|
91
|
+
t.string "pay_type"
|
92
|
+
|
93
|
+
t.datetime "created_at", null: false
|
94
|
+
|
95
|
+
t.datetime "updated_at", null: false
|
96
|
+
|
97
|
+
t.integer "user_id"
|
98
|
+
|
99
|
+
t.integer "product_id"
|
100
|
+
|
101
|
+
t.integer "quantity"
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
create_table "products", force: :cascade do |t|
|
108
|
+
|
109
|
+
t.string "title"
|
110
|
+
|
111
|
+
t.text "description"
|
112
|
+
|
113
|
+
t.string "image_url"
|
114
|
+
|
115
|
+
t.decimal "price", precision: 8, scale: 2
|
116
|
+
|
117
|
+
t.datetime "created_at", null: false
|
118
|
+
|
119
|
+
t.datetime "updated_at", null: false
|
120
|
+
|
121
|
+
t.string "category_id"
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
create_table "users", force: :cascade do |t|
|
128
|
+
|
129
|
+
t.string "name"
|
130
|
+
|
131
|
+
t.string "password_digest"
|
132
|
+
|
133
|
+
t.datetime "created_at", null: false
|
134
|
+
|
135
|
+
t.datetime "updated_at", null: false
|
136
|
+
|
137
|
+
t.string "email"
|
138
|
+
|
139
|
+
t.string "reset_digest"
|
140
|
+
|
141
|
+
t.datetime "reset_sent_at"
|
142
|
+
|
143
|
+
t.boolean "admin", default: false
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
line_item.rb
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
class LineItem < ApplicationRecord
|
158
|
+
|
159
|
+
belongs_to :order
|
160
|
+
|
161
|
+
belongs_to :product
|
162
|
+
|
163
|
+
belongs_to :cart
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
def total_price
|
168
|
+
|
169
|
+
product.price * quantity
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
product.rb
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
class Product < ApplicationRecord
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
has_many :line_items
|
186
|
+
|
187
|
+
has_many :orders, through: :line_items
|
188
|
+
|
189
|
+
has_many :users, through: :favorites
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
def total_count
|
194
|
+
|
195
|
+
line_items.to_a.sum { |item| item.quantity}
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
```
|
202
|
+
|
203
|
+
order.rb
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
class Order < ApplicationRecord
|
208
|
+
|
209
|
+
has_many :line_items, dependent: :destroy
|
210
|
+
|
211
|
+
belongs_to :user
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def add_line_items_from_cart(cart)
|
216
|
+
|
217
|
+
cart.line_items.each do |item|
|
218
|
+
|
219
|
+
item.cart_id = nil
|
220
|
+
|
221
|
+
line_items << item
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
def total_price
|
228
|
+
|
229
|
+
line_items.to_a.sum { |item| item.total_price }
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
def total_count
|
234
|
+
|
235
|
+
line_items.to_a.sum { |item| item.quantity}
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
以上宜しくお願いしますm(._.)m
|