2017-01-09 93 views
-1

告诉我如何建立模型之间的关系如下:
- 你可以有很多帖子
- 其他用户可以写在墙壁上相互位置(如社会。网络上,即,当你自己可以创建一个记录,也可以其他用户页面上创建如何建立正确的关联

回答

0

你至少应该尝试自己做,但这里是解决方案:
用户模式:

User (id, name) 
has_many :posts 
has_many :comments 
has_many :commented_posts, through: :comments 

发布模型:

Post (id, content, user_id) 
belongs_to :user 
has_many :comments 

评价模型:

Comment (id, content, post_id, user_id) 
belongs_to :user 
belongs_to :post 
0

如果refereing的职位与哪些用户可以标记其他用户,从而使后可以挂在墙上显示介质。

您可以定义使用帖子写入其他人的墙壁。这就是说任何社交平台的工作原理,同一篇文章可以像博客用户一样独立发布,可以为他的追随者或特定社区发布。

我是一个像平台的社交饲料,标签上的帖子是用户推送任何用户的墙上饲料的帖子的唯一方法。

所以在这里我们可以有以下实体。

用户

class User < ActiveRecord::Base 

    has_many :usertags  
    has_many :posts   
end 

class Post < ApplicationRecord 

    has_many :usertags, as: :usertagable 
    belongs_to :user 
    has_many :comments ,:as => :commentable 

end 

Usertag

class Usertag < ApplicationRecord 


    belongs_to :user 
    belongs_to :usertagable, :polymorphic => true 

end 

我已经建立usertags多态的关系,你可以扩展当前的架构涉及对文章的留言,以及像下面的评论模型一样,可以使用多态关系服务。

class Comment < ApplicationRecord 
    # all the relations for the comment 
    belongs_to :user 
    belongs_to :post 
    belongs_to :commentable, :polymorphic => true 
    has_many :comments, :as => :commentable 
    has_many :usertags, as: :usertagable 
end 

评论反过来属于用户/作者,评论附加的评论,评论也可以评论,因此它也可以属于可评论的。此外,评论可以让用户提到,就像帖子可以。

像社交饲料一样的平台,在帖子上标记用户是推动任何用户的贴子上的帖子的唯一方式。

现在,您可以轻松地获取属于特定用户的所有帖子以及评论的注释和评论。

post_list = Post.eager_load(:followers, :user, :communities, :usertags => :user, :comments => [:usertags => :user]).select("*").where("user.id is ?", :user_id) 

希望这会有所帮助 谢谢。

+0

我已经编辑了我的答案和更详细的信息,关于如何使用它来写入其他用户的feed/wall。希望能帮助到你。 – user3775217