質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

2回答

4359閲覧

Ruby on Rails 会話一覧で最新のメッセージを一件だけ表示させたい

riamk

総合スコア47

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

2クリップ

投稿2017/03/29 03:59

###前提・実現したいこと
rails4 ローカル環境

現在メッセージ機能を実装してラインのようなメッセージのやりとりができるようにしたいと思っています。

機能自体は実装できたのですが、会話一覧ページ(誰とメッセージのやり取りをしているか表示するラインのトーク一覧のようなページ)で最新のメッセージを一件取得する方法が分かりません。

conversations : 会話情報を格納するテーブル
messages : 会話の個々のメッセージを格納するテーブル
として実装しました。

###該当のソースコード
conversations/index.html.erbここで最新のメッセージを一件だけ取得したい

<div class="container message"> <div class="wrapper col-md-8 col-md-offset-2 col-sm-10 message-index"> <h2>メッセージ一覧</h2> <% @conversations.each do |conversation| %> <div class="row conversation-list"> <% if conversation.target_user(current_user).present? %> <%= link_to conversation.target_user(current_user).name, conversation_messages_path(conversation)%> <% end %> </div> <% end %> </div> </div>

messages_controller.rb

messages_controller.rb

1class MessagesController < ApplicationController 2 before_action :authenticate_user! 3 4 before_action do 5 @conversation = Conversation.find(params[:conversation_id]) 6 end 7 8 def index 9 @messages = @conversation.messages 10 if @messages.length > 10 11 @over_ten = true 12 @messages = @messages[-10..-1] 13 end 14 15 if params[:m] 16 @over_ten = false 17 @messages = @conversation.messages 18 end 19 20 if @messages.last 21 if @messages.last.user_id != current_user.id 22 @messages.last.read = true 23 end 24 end 25 26 @message = @conversation.messages.build 27 end 28 29 def create 30 @message = @conversation.messages.build(message_params) 31 if @message.save 32 redirect_to conversation_messages_path(@conversation) 33 end 34 end 35 36 private 37 def message_params 38 params.require(:message).permit(:body, :user_id) 39 end

messageモデル

class Message < ActiveRecord::Base belongs_to :conversation belongs_to :user validates_presence_of :body, :conversation_id, :user_id def message_time created_at.strftime("%m/%d/%y at %l:%M %p") end end

conversations_controller.rb

class ConversationsController < ApplicationController before_action :authenticate_user! def index @users = User.all @conversations = Conversation.all end def create if Conversation.between(params[:sender_id], params[:recipient_id]).present? @conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first else @conversation = Conversation.create!(conversation_params) end redirect_to conversation_messages_path(@conversation) end private def conversation_params params.permit(:sender_id, :recipient_id) end end

Conversationモデル

class Conversation < ActiveRecord::Base belongs_to :sender, foreign_key: :sender_id, class_name: 'User' belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User' has_many :messages, dependent: :destroy validates_uniqueness_of :sender_id, scope: :recipient_id scope :between, -> (sender_id,recipient_id) do where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id) end def target_user(current_user) if sender_id == current_user.id User.find(recipient_id) elsif recipient_id == current_user.id User.find(sender_id) end end end

データベーステーブル

create_table "messages", force: :cascade do |t| t.text "body" t.integer "conversation_id" t.integer "user_id" t.boolean "read", default: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "messages", ["conversation_id"], name: "index_messages_on_conversation_id", using: :btree add_index "messages", ["user_id"], name: "index_messages_on_user_id", using: :btree create_table "conversations", force: :cascade do |t| t.integer "sender_id" t.integer "recipient_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end

どのように最新のメッセージを一件取得するばいいかアドバイス頂けると嬉しいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

もうベストアンサーついてますが、こんなやり方もあるよという意味で回答します。

@messages.order(created_at: :desc).take

また、find_byとorderを組み合わせて使用することで、1件のみ取得することも可能です。

投稿2017/03/29 05:46

KaiShoya

総合スコア551

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

riamk

2017/03/29 05:56

ご回答ありがとうございます。 サブクエリを色々調べていたのですが、うまく記述できず困っていたので、KaiShoyaさんの回答を参考にこちらも試してみます!
退会済みユーザー

退会済みユーザー

2017/03/29 06:31

横からすみません。 @mesagesに何が入っているのかわかりませんが、この方法だとすべてのmessageのうちの最新のものしか取得できないように見えます。 質問者さんはLINEのトーク一覧のようなものを作りたいそうなので、各conversationで最新のmessageを取得する必要がありそうに思いました。(間違ってたらすみません...)
KaiShoya

2017/03/29 06:44 編集

あー、確かにそうですね。 メッセージ機能は実装済みとのことなので、トーク別でのメッセージ取得はできている前提で話してました。 conversations/index.html.erbを見直しましたが、こんな感じでしょうか。 ``` <% @conversations.each do |conversation| %> <%= conversation.messages.order(created_at: :desc).take %> <% end %> ```
riamk

2017/03/29 08:44

実現したいのはchromeさんが仰るように「各conversationで最新のmessageを取得する」です。 私の説明が伝わりづらくてすみません。 教えて頂いた <%= conversation.messages.order(created_at: :desc).take %> で試してみたところ、 #<Message:0x007fdab6718200> と表示されてしまい、メッセージの内容(body)を取得できません。 原因はどこにあるんでしょうか?
guest

0

ベストアンサー

サブクエリを使えばいけると思います。

conversations_controller.rbindex内で

ruby

1@messages = Message.where(%" 2 messages.created_at = ( 3 SELECT MAX(m_sub.created_at) 4 FROM messages m_sub 5 GROUP BY m_sub.conversation_id 6 )")

適当に書いたコードなのでこれそのままは使えないと思いますが、たぶんこんな感じでサブクエリを使えば各conversationでcreated_atが最新のmessage一覧が取得できると思います。

あとはconversations/index.html.erb

<% @conversations.each do |conversation| %> <%= @message.find{|m| m.conversation_id == conversation.id}.body %> <% end %>

みたいにすればいけそうな気がするのですがどうでしょうか。

投稿2017/03/29 04:40

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

riamk

2017/03/29 05:30

ご回答ありがとうございます。 サブクエリはまだ使用したことがないので、色々と調べながら試してみます! ありがとうございました。
riamk

2017/03/29 05:32

ご回答ありがとうございます。 サブクエリは使用したことがなかったので色々と調べながら試してみます。 ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問