teratailのお知らせ一覧のような機能を作ったのですが、我ながらクソだな。。と思い作り直したいのですが、アドバイスを下さい
現状
itemに質問などのアクションが起きた際に、notificationをレコードを発行し、それをnotification contorollerで集めて、view側で対応する文言を当てはめています。
問題点
① notificationのレコード数が10万、100万になった場合のパフォーマンス
(多分まともに動かない気がする)
② 未読数を表示するためにapplication.html.erbに書いたコードが毎回呼び出されるのでパフォーマンスが。。
③view側で文言を振り分けているのがよくない気がする。。がどうしていいかわからない
lang
1#application.html.erb 2<!-- お知らせ一覧 --> 3 <li> 4 <a href="/notifications/"> 5 <i class="fa fa-bell-o"></i> 6 <!-- Indicator with number --> 7 <% if @recent_notifications_activity.present? %> 8 <span class="navbar-new"><%= @recent_notifications_activity.count %></span> 9 <% end %> 10 </a> 11 </li> 12 13 14 15#notifications/index.html.erb 16<% @notifications.each do |comment| %> 17 18<!-- 質問 --> 19 <% if comment.body_num == 10000 %> 20 <a href="<%= item_url(comment.item_id) %>" class="list-group-item"> 21  <i class="fa fa-comment-o"></i>  22 <small>アイテムに<%= comment.item.title%>に<%= comment.originator.profile.user_name%>さんが質問しました。</small> 23 <p class="palette-paragraph text-right"><small><i class="fa fa-clock-o"></i> <%=date_format(comment.created_at) %></small></p> 24 </a> 25 <% end %> 26<!-- アイテムが更新されました --> 27<!-- 削除されました。 --> 28<% end %>
lang
1#notification controller 2class NotificationsController < ApplicationController 3 4 # GET /notifications 5 # GET /notifications.json 6 def index 7 @user = current_user.id 8 @notifications = Notification.where("(recipient_id = ?) OR (body_num = ?)", @user, 80000) 9 .order(created_at: :desc) 10 .includes(:item).includes(:originator => :profile) 11 .page(params[:page]).per(15) 12 #アクセスフラグ 13 @notifications.update_all({status: true}) 14 end 15 16 private 17 # Never trust parameters from the scary internet, only allow the white list through. 18 def notification_params 19 params.require(:notification).permit(:status, :originator_id, :recipient_id, :item_id, :navi_id) 20 end 21end 22
lang
1#notification db migrate 2class CreateNotifications < ActiveRecord::Migration 3 def change 4 create_table :notifications do |t| 5 t.integer :originator_id # 発信者のuser_id 6 t.integer :recipient_id # 受信者のuser_id 7 t.integer :item_id # アイテム 8 t.integer :body_num # 対応する文言 9 t.boolean :status # 閲覧フラグ 10 t.timestamps 11 end 12 add_index :notifications, :item_id 13 add_index :notifications, :originator_id 14 add_index :notifications, :recipient_id 15 end 16end 17
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2015/07/06 12:26