Rails mongoid 怎么实现对多的单向多态?

xranthoar · May 21, 2012 · 2877 hits

也就是实现一个 A 指向许多个 B 并且这种关系是多态的,但不需要 B 这一边和 A 建立关系,开发中遇到的问题是这样的。为了收集消息 (Notification, embed 在 User 或 Organization 里) 发送的情况,建立了一个名为 NotifRecord 的表:

class NotifRecord
  include Mongoid::Document
  include Mongoid::Timestamps

  field :content
  field :type

  belongs_to :notifier, polymorphic: true
  has_many :to_notify_orgs, inverse_of: nil, class_name: 'User'
  has_many :to_notify_users, inverse_of: nil, class_name: 'Organization'
  has_many :read_by_users, inverse_of: nil, class_name: 'User'
  has_many :read_by_orgs, inverse_of: nil, class_name: 'Organization'
end

想让其储存发通知的对象和已读通知的对象,在这里对象即可能是组织 (Organization) 也可能是用户 (User),有什么办法实现 has_many :to_notify 和 has_many :read_by 而不用指定 class_name 呢? 谢谢解答

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.