質問編集履歴
4
タイトルをよりわかりやすいものに変更しました!
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Rails
|
1
|
+
Rails データの並び順を関連付けされたテーブル(messageテーブル)のデータで並べ替える
|
body
CHANGED
File without changes
|
3
リレーションを追記しました!
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,6 +11,7 @@
|
|
11
11
|
messageテーブル
|
12
12
|
group_usersテーブル(中間テーブルでuser_idとgroup_idが入っています)
|
13
13
|
以上4テーブル
|
14
|
+
groupテーブルとmessageテーブルのリレーションはhas_manyとbelongs_toで繋げてあります!
|
14
15
|
|
15
16
|
messageはgroupにネストされております↓
|
16
17
|
```
|
2
タイトルをよりわかりやすいものに変更しました!
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
Rails グループ一覧の並び順を関連付けされたテーブル(messageテーブル)のデータで並べ替える
|
body
CHANGED
File without changes
|
1
アドバイスをもとにコードを変えました!
title
CHANGED
File without changes
|
body
CHANGED
@@ -23,7 +23,29 @@
|
|
23
23
|
groups_controller.rb↓
|
24
24
|
```
|
25
25
|
def index
|
26
|
-
@groups = Group.
|
26
|
+
@groups = Group.includes(:message).order("messages.created_at DESC")
|
27
|
+
binding.pry →=> #<Group::ActiveRecord_Relation:0x3fc419ff6ca8>#何も入っていない??
|
27
28
|
end
|
28
29
|
|
29
|
-
```
|
30
|
+
```
|
31
|
+
上記のコードで試しましたが、エラーになってしまいます、、、
|
32
|
+
|
33
|
+
###エラー文↓
|
34
|
+
ActiveRecord::ConfigurationError in Groups#index
|
35
|
+
|
36
|
+
Showing /Users/kounotakumi/projects/tentyo-chat/app/views/layouts/_group_index.html.erb where line #10 raised:
|
37
|
+
|
38
|
+
Can't join 'Group' to association named 'message'; perhaps you misspelled it?→上手くjoinできていない??
|
39
|
+
|
40
|
+
Extracted source (around line #10):
|
41
|
+
```
|
42
|
+
8 <% end %>
|
43
|
+
9 </div>
|
44
|
+
10 <% groups.each do |group| %>
|
45
|
+
11 <div class="Main__left__group">
|
46
|
+
12 <%= link_to(group_messages_path(group), class: 'Main__left__group-a') do %>
|
47
|
+
13 <div class="Main__left__group-name">
|
48
|
+
```
|
49
|
+
|
50
|
+
|
51
|
+
Trace of template inclusion: #<ActionView::Template app/views/groups/index.html.erb locals=[]>
|