質問編集履歴
2
内容の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -9,8 +9,8 @@
|
|
9
9
|
|
10
10
|
<div class='col-md-8 offset-md-1'>
|
11
11
|
<h2>Books</h2>
|
12
|
-
<%= link_to "新しい順", books_path(create
|
12
|
+
<%= link_to "新しい順", books_path(sort_create: "true") %> |
|
13
|
-
<%= link_to "評価の高い順", books_path(star:
|
13
|
+
<%= link_to "評価の高い順", books_path(sort_star: "true") %> |
|
14
14
|
<%= link_to "いいねの多い順", books_path(sort: 'favorite DESC') %>
|
15
15
|
<%= render 'index', books: @books %>
|
16
16
|
</div>
|
@@ -19,12 +19,21 @@
|
|
19
19
|
``` controller
|
20
20
|
def index
|
21
21
|
@books = Book.most_favorite.order("favorites_count DESC").select("books.*")
|
22
|
+
if params[:sort_create]
|
23
|
+
@books = Book.latest
|
24
|
+
elsif params[:sort_star]
|
25
|
+
@books = Book.star
|
26
|
+
end
|
22
27
|
@user=current_user
|
23
28
|
@book=Book.new
|
24
29
|
end
|
25
30
|
|
26
31
|
```model
|
32
|
+
scope :latest, -> {order(created_at: :desc)}
|
33
|
+
scope :star, -> {order(star: :desc)}
|
34
|
+
|
27
35
|
has_many :favorites, dependent: :destroy
|
28
36
|
has_many :week_favorites, -> { where(created_at: ((Time.current.at_end_of_day - 6.day).
|
29
37
|
at_beginning_of_day)..(Time.current.at_end_of_day)) }, class_name: 'Favorite'
|
38
|
+
|
30
39
|
scope :most_favorite, -> { left_joins(:favorites).select(:id, "COUNT(favorites.id) AS favorites_count").group(:id) }
|
1
情報の追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
railsの並び替えで、デフォルトはいいねの多い順、リンクをクリックすることで新着順と評価の高い順にそれぞれソートしたい
|
1
|
+
railsの並び替えで、デフォルトはいいねの多い順、リンクをクリックすることで新着順と評価の高い順、いいねの多い順にそれぞれソートしたい
|
test
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
ビューとコントローラの記述で詰まっています。
|
4
4
|
初学者ですがよろしくお願いいたします。
|
5
5
|
|
6
|
-
|
6
|
+
Bookモデルには評価(star)カラムはありますが,いいねのカラムは作ってないです。
|
7
7
|
|
8
8
|
```rails
|
9
9
|
|
@@ -24,4 +24,7 @@
|
|
24
24
|
end
|
25
25
|
|
26
26
|
```model
|
27
|
+
has_many :favorites, dependent: :destroy
|
28
|
+
has_many :week_favorites, -> { where(created_at: ((Time.current.at_end_of_day - 6.day).
|
29
|
+
at_beginning_of_day)..(Time.current.at_end_of_day)) }, class_name: 'Favorite'
|
27
30
|
scope :most_favorite, -> { left_joins(:favorites).select(:id, "COUNT(favorites.id) AS favorites_count").group(:id) }
|