如何设置两层以上的关联?我有一个资源用户,has_one博客,其中有许多帖子,其中有许多评论,has_one联系。已有用户has_may联系人。但是我怎么处理剩下的呢?用户应该通过=>博客发布have_many帖子吗?或者我应该直接使用Post reference User?
发布于 2012-07-20 00:41:28
有了许多可能的直接关联和直通关联,您有很多选择,并且没有任何严格的规则。所以设置好你需要的东西。你确实有很多深度的联想,但是你真的想要user.posts吗,因为它们会出现在不同的博客上?不管怎样,我在下面添加了它。
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 :commentshttps://stackoverflow.com/questions/11565238
复制相似问题