0

I'm trying to create notifications in rails 5 with action cable. Wondering if anyone could help with my troubles .

Currently I have my notifications table

Schema

    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

My Notification Model

  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

I'm having trouble understanding how to represent the user notifications within a controller and views.

For example I have the notification create method when a specific action is created.

  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

Here I attempt to tie the User & Notifications together within a helper method in application controller.

 helper_method :current_notifications

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

The error I receive is

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

My problem again lies within how to represent the user's notifications. I'm pretty sure I'm confused on how to tie things together. I was attempting to follow Chris Oliver tutorial which I listed below.

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

Any help or corrections would be helpful

Nithin
  • 3,679
  • 3
  • 30
  • 55
leafshinobi25
  • 67
  • 2
  • 6

2 Answers2

0

In Comment:

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

I'm wondering if the error is coming from passing the entire user object to user_id and recipient_id rather than the id itself?

  • That might be a reason, the issue initially seems to form when trying to express the relationship between the User & Notification within the application controller. I haven't even been able to get to the point where I'm actually testing the action of creating the notification. @ReidasauresRex – leafshinobi25 Oct 27 '16 at 10:49
0

A little more critical thinking allowed me to answer my issue at least so far.

Within the application controller I ended up using

def current_notifications

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

So far that has solved the error I have been recieving

leafshinobi25
  • 67
  • 2
  • 6