【发布时间】:2015-10-09 17:26:50
【问题描述】:
我刚刚在我的应用中添加了一些通知功能。但是,当我在 rails admin 上检查通知时,它没有检测到关联的多态对象。 我只在开发中使用 rails admin.. 所以如果这是一个 rails admin 错误,那么我很乐意离开它。但是(并且很可能)如果我做错了什么,我需要修复它。
我应该提一下 - 这是在数据库中工作,并且在站点的任何地方都可以工作,除了 rails admin
我的模型
class Notification < ActiveRecord::Base
belongs_to :notified, polymorphic: true
belongs_to :object, polymorphic: true
belongs_to :user
scope :not_seen, -> { where(seen: false) }
scope :not_clicked, -> { where(clicked: false) }
def self.send_notifications(user, message, object, subscribers, mailer = nil, mailer_object = nil)
subscribers.uniq.each do |subscriber|
self.create({user: user, message: message, notified: subscriber, object: object}) unless subscriber.get_user_id == user.id
UserMailer.send(mailer, subscriber, mailer_object).deliver unless mailer.nil? || subscriber.get_user_id == user.id
end
end
end
All_Models_that_can_be_notified_about.rb
class .... < ActiveRecord::Base
..
has_many :notifications, as: :object
..
end
这是创建通知的行
self.create({user: user, message: message, notified: subscriber, object: object}) unless subscriber.get_user_id == user.id
这是控制台上的样子:
<Notification id: 7, user_id: 1, message: " has left a comment on ", notified_id: 2, notified_type: "Programme", created_at: "2015-10-09 12:51:07", updated_at: "2015-10-09 12:51:07", seen: true, clicked: true, object_id: 54, object_type: "Applicant">
从上面可以看到,object_id 和 object_type 已填充(54,申请人)
但在 Rails 管理员中,我得到了这个:
它检测到模型是申请人,但它没有看到 ID
有什么想法吗?
【问题讨论】:
-
object是一个不幸的名称选择,因为#object_id已经被定义为 Ruby 对象的方法。我不知道这是否是问题的根源,但您可以考虑将object更改为其他内容以避免潜在问题。 -
谢谢!这就是问题的根源。如果您可以将此作为答案,我将很乐意接受。
-
我也很怀疑。名称冲突可能很难调试。
标签: ruby-on-rails polymorphic-associations rails-admin ruby-on-rails-4.1