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