Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何创建正确的关联_Ruby On Rails_Associations - Fatal编程技术网

Ruby on rails 如何创建正确的关联

Ruby on rails 如何创建正确的关联,ruby-on-rails,associations,Ruby On Rails,Associations,告诉我如何在模型之间建立关系,如下所示: -你可以有很多帖子 -其他用户可以在墙上的其他位置相互写信(如在社交网络中,即当您可以自己创建记录或在另一个用户页面上创建记录时)。您至少应该自己尝试,但这里有一个解决方案: 用户模型: User (id, name) has_many :posts has_many :comments has_many :commented_posts, through: :comments 后模型: Post (id, content, user_id)

告诉我如何在模型之间建立关系,如下所示:
-你可以有很多帖子

-其他用户可以在墙上的其他位置相互写信(如在社交网络中,即当您可以自己创建记录或在另一个用户页面上创建记录时)。

您至少应该自己尝试,但这里有一个解决方案:
用户模型:

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

如果您将帖子称为用户可以标记其他用户的媒介,以便帖子可以显示在他们的墙上

你可以明确地使用帖子给其他人写信。这就是任何社交平台的工作原理,同一篇帖子可以作为独立的帖子,就像博客用户为其追随者或特定社区发布一样

我是一个类似于社会化提要的平台,在帖子上标记用户是将帖子推到任何用户的墙上提要的唯一方法

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

使用者

希望这有帮助
谢谢。

我已经用更详细的信息编辑了我的答案,告诉你如何使用它在其他用户的feed/wall上写作。希望能有所帮助。
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
class Usertag < ApplicationRecord


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

end
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)