Ruby on rails 用于相当简单的通知系统的Rails关联

Ruby on rails 用于相当简单的通知系统的Rails关联,ruby-on-rails,database-design,mongoid,associations,modeling,Ruby On Rails,Database Design,Mongoid,Associations,Modeling,我正在尝试在Rails和mongoid中建立一个通知系统(但我不认为这是mongoid特有的) 基本结构是这样的——每个通知都有一个通知者(负责通知的人)和一个notifee(接收通知的人)。当用户a对用户B的帖子(例如在博客系统中)发表评论时,用户a成为通知者,用户B成为通知者 User.rb # nothing in here has_one :notifier, :class_name => "User" belongs_to :notifiee, :class_name =>

我正在尝试在Rails和mongoid中建立一个通知系统(但我不认为这是mongoid特有的)

基本结构是这样的——每个通知都有一个通知者(负责通知的人)和一个notifee(接收通知的人)。当用户a对用户B的帖子(例如在博客系统中)发表评论时,用户a成为通知者,用户B成为通知者

User.rb

# nothing in here
has_one :notifier, :class_name => "User"
belongs_to :notifiee, :class_name => "User"
通知.rb

# nothing in here
has_one :notifier, :class_name => "User"
belongs_to :notifiee, :class_name => "User"
然而,当我这样做时:

@notification = Notification.new
@notification.notifier = current_user
@notification.notifiee = User.first #Just for the sake of the example
@notification.save
我得到这个错误:

问题:向通知程序添加(n)个用户时,Mongoid可能会 不确定要设置的反向外键。尝试的密钥无效 “Notified_id”。摘要:将文档添加到关系时,Mongoid 尝试将新添加的文档链接到关系的基础 在内存中,以及设置外键以将它们链接到数据库上 一边在这种情况下,Mongoid无法确定反向是什么 外键为。分辨率:如果不需要反转,如 属于或拥有且属于多个,确保:逆的=>nil 是建立在关系上的。如果需要相反的结果,最有可能是 不能从关系的名称和您的关系中找出反向 需要明确地告诉Mongoid关于关系的逆 是

我可能做错了什么?或者,有没有更好的方法来建模


非常感谢您的帮助!谢谢。

您可能应该选择以下关联:

用户:

通知:

belongs_to :notifier, :class_name=>'User', :foreign_key=>'notifier_id'
belongs_to :notifiee, :class_name=>'User', :foreign_key=>'notifiee_id'
您的
通知
表应具有
通知者id
通知者id

现在你可以做了

@notification = Notification.new
@notification.notifier = current_user
@notification.notifiee = User.first #Just for the sake of the example
@notification.save

我发现您的设置有问题:

你有

has_one :notifier, :class_name => "User"
belongs_to :notifiee, :class_name => "User"
使用
has_on
时,另一个关系(表)必须具有引用父关系的外键。这里的
用户
必须有一列
通知id
或其他内容。这是不切实际的,因为单个用户有许多通知(根据您的解释)

其次,您通过两个关系将通知关联到用户,但是您提到了用于强制关联的外键的任何内容


为什么在用户模型中没有反向关系?如果您可以访问类似以下内容:当前用户。通知作为通知程序?

您可能应该选择以下关联:

用户:

通知:

belongs_to :notifier, :class_name=>'User', :foreign_key=>'notifier_id'
belongs_to :notifiee, :class_name=>'User', :foreign_key=>'notifiee_id'
您的
通知
表应具有
通知者id
通知者id

现在你可以做了

@notification = Notification.new
@notification.notifier = current_user
@notification.notifiee = User.first #Just for the sake of the example
@notification.save

我发现您的设置有问题:

你有

has_one :notifier, :class_name => "User"
belongs_to :notifiee, :class_name => "User"
使用
has_on
时,另一个关系(表)必须具有引用父关系的外键。这里的
用户
必须有一列
通知id
或其他内容。这是不切实际的,因为单个用户有许多通知(根据您的解释)

其次,您通过两个关系将通知关联到用户,但是您提到了用于强制关联的外键的任何内容


为什么在用户模型中没有反向关系?如果您可以访问类似以下内容:当前用户。通知作为通知程序?

您可能应该选择以下关联:

用户:

通知:

belongs_to :notifier, :class_name=>'User', :foreign_key=>'notifier_id'
belongs_to :notifiee, :class_name=>'User', :foreign_key=>'notifiee_id'
您的
通知
表应具有
通知者id
通知者id

现在你可以做了

@notification = Notification.new
@notification.notifier = current_user
@notification.notifiee = User.first #Just for the sake of the example
@notification.save

我发现您的设置有问题:

你有

has_one :notifier, :class_name => "User"
belongs_to :notifiee, :class_name => "User"
使用
has_on
时,另一个关系(表)必须具有引用父关系的外键。这里的
用户
必须有一列
通知id
或其他内容。这是不切实际的,因为单个用户有许多通知(根据您的解释)

其次,您通过两个关系将通知关联到用户,但是您提到了用于强制关联的外键的任何内容


为什么在用户模型中没有反向关系?如果您可以访问类似以下内容:当前用户。通知作为通知程序?

您可能应该选择以下关联:

用户:

通知:

belongs_to :notifier, :class_name=>'User', :foreign_key=>'notifier_id'
belongs_to :notifiee, :class_name=>'User', :foreign_key=>'notifiee_id'
您的
通知
表应具有
通知者id
通知者id

现在你可以做了

@notification = Notification.new
@notification.notifier = current_user
@notification.notifiee = User.first #Just for the sake of the example
@notification.save

我发现您的设置有问题:

你有

has_one :notifier, :class_name => "User"
belongs_to :notifiee, :class_name => "User"
使用
has_on
时,另一个关系(表)必须具有引用父关系的外键。这里的
用户
必须有一列
通知id
或其他内容。这是不切实际的,因为单个用户有许多通知(根据您的解释)

其次,您通过两个关系将通知关联到用户,但是您提到了用于强制关联的外键的任何内容


为什么在用户模型中没有反向关系?如果您有权访问以下内容,则不会有帮助:
current\u user.notifications\u as\u notifier
??

发布通知模式。错误也很明显。您不需要反向关系,因此将其设置为nil.post schema进行通知。错误也很明显。您不需要反向关系,因此将其设置为nil.post schema进行通知。错误也很明显。您不需要反向关系,因此将其设置为nil.post schema进行通知。错误也很明显。您不需要反向关系,所以将其设置为零。谢谢,您是对的,我认为将当前的用户通知设置为通知将非常好