蒙古人协会

发布于 2024-10-17 16:59:00 字数 512 浏览 1 评论 0原文

我遇到一个问题,我试图从评论模型中引用一个名为 user 的模型两次。

第一个实例是因为一个用户可以有许多评论(作为收件人),

但另一个关联是评论有一个作者。那

references_one :user, :inverse_of :author

我是否需要用户中的另一个关联来获取该用户的评论,而不是该用户的评论。

embedded_in :user, :inverse_of => :commentsout

我希望这有点道理。

也许这样的事情更有意义(伪代码)

user:
   has_many: comments => author
   can_be: author, recipient

comment:
   belongs_to_many: users => recipients
   has_one: user => author

I'm having a problem where i'm trying to reference a model called user twice from a comment model.

The first instance is because a user can have many comments (as recipient)

But the other association is a comment has one author. Is that

references_one :user, :inverse_of :author

Would I then need another association in user for comments by that user as opposed to for that user.

embedded_in :user, :inverse_of => :commentsout

I hope it makes a bit of sense.

Maybe something like this makes more sense (pseudo-code)

user:
   has_many: comments => author
   can_be: author, recipient

comment:
   belongs_to_many: users => recipients
   has_one: user => author

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2024-10-24 16:59:00

如果我正确理解问题,您可以这样定义关联:

class User
  include Mongoid::Document      
  field :name,  :type => String      
  references_many :comments, :inverse_of => :author
  references_and_referenced_in_many :comments_received, :class_name => 'Comment', :inverse_of => :recipients
end

class Comment
  include Mongoid::Document    
  field :text, :type => String
  referenced_in :author, :class_name => 'User'
  references_and_referenced_in_many :recipients, :class_name => 'User', :inverse_of => :comments_received
end

如果无法从关联名称推断出目标,则需要添加 :class_name 参数。这使得同一个类可以有多个关联。

If I understand the problem correctly you can define the associations like this:

class User
  include Mongoid::Document      
  field :name,  :type => String      
  references_many :comments, :inverse_of => :author
  references_and_referenced_in_many :comments_received, :class_name => 'Comment', :inverse_of => :recipients
end

class Comment
  include Mongoid::Document    
  field :text, :type => String
  referenced_in :author, :class_name => 'User'
  references_and_referenced_in_many :recipients, :class_name => 'User', :inverse_of => :comments_received
end

If the target cannot be inferred from the association name you need to add a :class_name parameter. This makes it possible to have multiple associations to the same class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文