2016-10-26 8 views
0

私は、アクションケーブルでレール5に通知を作成しようとしています。誰かが私の悩みを助けることができるかどうか分かります。Rails In App通知

現在、私は、私は、コントローラ内のユーザーの通知を表現する方法を理解する問題が発生したとしています私の通知表

スキーマ

create_table "notifications", force: :cascade do |t| 
    t.string "activity" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.integer "user_id" 
    t.integer "recipient_id" 
    t.string "action" 
    t.string "notifiable_type" 
    t.integer "notifiable_id" 
    t.index ["user_id"], name: "index_notifications_on_user_id" 
    end 

マイ通知モデル

class Notification < ApplicationRecord 
    belongs_to :user 
    belongs_to :recipient, class_name: "User" 
    belongs_to :notifiable, polymorphic: true 
    after_create_commit { NotificationBroadcastJob.perform_later(Notification.count,self)} 

validates :user_id, presence: true  
end 

を持っています再生回数

たとえば、特定のアクションが作成されたときに通知作成メソッドがあります。ここ

class Comment < ApplicationRecord 
    after_create_commit { create_notification } 

private 

def create_notification 
Notification.create action: "Your comment has been created", user_id: comment.user, recipient_id: comment.user 
end 
end 

Iは、アプリケーションコントローラでヘルパーメソッド内で一緒にユーザー&通知を結合しようと試みます。

helper_method :current_notifications 

def current_notifications 
if current_user 
@notifications = current_user.notifications.all 
else 
end 
end 

私が受け取るエラーは私の問題は、再びユーザーの通知を表現する方法の範囲内にある

SQLite3::SQLException: no such column: notifications.recipient_type: SELECT COUNT(*) FROM "notifications" WHERE "notifications"."recipient_id" = ? AND "notifications"."recipient_type" = ? 

です。私は一緒に物事を結びつける方法を私が混乱していると確信しています。私は下にリストしたクリス・オリバーのチュートリアルに従おうとしていました。

https://gist.github.com/excid3/4ca7cbead79f06365424b98fa7f8ecf6

すべてのヘルプや修正はコメントで役立つ

答えて

0

次のようになります。

def create_notification 
    Notification.create action: "Your comment has been created", user_id: comment.user.id, recipient_id: comment.user.id 
end 

エラーが全体ではなくuserオブジェクトを渡すからuser_idrecipient_idに来ている場合、私は思ったんだけどid自体よりも?

+0

が供給されているエラーを解決したこと、問題が最初に形成するように思わ

def current_notifications if current_user @notifications = Notification.all.where(user_id: [current_user.id]) else end end 

を使用して終了したアプリケーションのコントローラ内の

アプリケーションコントローラ内のユーザーと通知の関係私は実際に通知を作成する動作を実際にテストしているところまで到達することさえできませんでした。 @ReidasauresRex – leafshinobi25

0

もう少し批判的な考え方は、私が少なくとも今まで私の問題に答えることを可能にしました。私が表現しようとするこれまでのところ、私は理由かもしれません

関連する問題