蒙古人协会
我遇到一个问题,我试图从评论模型中引用一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解问题,您可以这样定义关联:
如果无法从关联名称推断出目标,则需要添加 :class_name 参数。这使得同一个类可以有多个关联。
If I understand the problem correctly you can define the associations like this:
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.