【发布时间】:2016-02-13 10:47:02
【问题描述】:
我有三个模型,即通知、设备和用户。
通知有registration_ids 映射到设备的token 字段。 Devices 有 user_id 字段,该字段映射到 User 模型的 id 字段。
如何从 Notification 模型创建 as has_many_through 或 has_and_belongs_to_many 关联以提取与该通知对应的用户。
此外,我在通知类 belongs_to :device, :primary_key => 'registration_ids', :foreign_key => 'token' 中创建了这个关联,而在设备类 has_many :notifications, :primary_key => 'token', :foreign_key => 'registration_ids' 中创建了这个关联
设备类能够识别通知类,而通知类不能识别设备类
从 notification.rb 文件中查看我的代码
class Notification < Rpush::Gcm::Notification
self.inheritance_column = nil
belongs_to :device, :foreign_key => 'registration_ids', :primary_key => 'token'
delegate :user, to: :device #-> notification.user.name
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 associations