首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模型四层深度-- Rails 3

模型四层深度-- Rails 3
EN

Stack Overflow用户
提问于 2012-07-20 00:28:52
回答 1查看 45关注 0票数 0

如何设置两层以上的关联?我有一个资源用户,has_one博客,其中有许多帖子,其中有许多评论,has_one联系。已有用户has_may联系人。但是我怎么处理剩下的呢?用户应该通过=>博客发布have_many帖子吗?或者我应该直接使用Post reference User?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-20 00:41:28

有了许多可能的直接关联和直通关联,您有很多选择,并且没有任何严格的规则。所以设置好你需要的东西。你确实有很多深度的联想,但是你真的想要user.posts吗,因为它们会出现在不同的博客上?不管怎样,我在下面添加了它。

代码语言:javascript
复制
class User
  has_many :blogs
  has_many :contacts, :through => :user_contacts
  has_many :posts, :through => :blogs

class UserContacts
  belongs_to :user
  belongs_to :contact

class Blog
  belongs_to :user
  has_many :posts

class Post
  belongs_to :blog
  has_many :comments

class Comment
  belongs_to :post
  has_one :contact

class Contact
  has_many :comments
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11565238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档