【问题标题】:SQLite3 exception in action cable动作电缆中的 SQLite3 异常
【发布时间】:2016-10-26 22:23:27
【问题描述】:

我正在尝试使用操作电缆在 rails 5 中创建通知。想知道是否有人可以帮助解决我的问题。

目前我有我的通知表

架构

    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

在这里,我尝试在应用程序控制器的辅助方法中将用户和通知联系在一起。

 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" = ?

我的问题再次在于如何表示用户的通知。我很确定我对如何将事情联系在一起感到困惑。我正在尝试遵循我在下面列出的 Chris Oliver 教程。

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

任何帮助或更正都会有所帮助

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 actioncable


    【解决方案1】:

    在评论中:

    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 本身?

    【讨论】:

    • 这可能是一个原因,最初似乎是在尝试表达应用程序控制器中的用户和通知之间的关系时形成的。我什至还没有达到我实际测试创建通知操作的地步。 @ReidasauresRex
    【解决方案2】:

    至少到目前为止,多一点批判性思维让我能够回答我的问题。

    在应用程序控制器中我最终使用了

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

    到目前为止,这已经解决了我收到的错误

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 2016-11-23
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2017-05-27
      相关资源
      最近更新 更多