Ruby on rails rails中两个模型之间的多重关系

Ruby on rails rails中两个模型之间的多重关系,ruby-on-rails,Ruby On Rails,我有一个带有两个表的评论系统:评论和用户。在评论中,我想记录作者是谁,并且我想用(@username)通知评论中提到的任何用户。所以我想我需要在评论上有一个作者id,还有一个带有评论id和所有提到的用户id的评论用户表。这是实现这一目标的正确方法吗 用户: 评论: belongs_to :users, class_name: 'User', foreign_key: 'author_id' has_many :users belongs_to :author, class_name: 'Use

我有一个带有两个表的评论系统:评论和用户。在评论中,我想记录作者是谁,并且我想用(@username)通知评论中提到的任何用户。所以我想我需要在评论上有一个作者id,还有一个带有评论id和所有提到的用户id的评论用户表。这是实现这一目标的正确方法吗

用户:

评论:

belongs_to :users, class_name: 'User', foreign_key: 'author_id'
has_many :users
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
has_many :users

可以这样建立协会:

#app/models/user.rb
Class User < ActiveRecord::Base
    has_many :comments
    has_and_belongs_to_many :mentions, join_table: "comments_users", association_foriegn_key: "comment_id"
end

Class Comment < ActiveRecord::Base
    belongs_to :author, class_name: "User", foreign_key: "author_id"
    has_and_belongs_to_many :mentions, join_table: "comments_users", foreign_key: "comment_id"
end

#comments_users
comment_id | user_id

更新

HABTM仍然需要一个表(),但区别在于它不需要主键列(只需
comment\u id | user\u id

我们已经创建了一个“自引用”的habtm关系,这意味着您不需要“创建”任何记录——它们都应该已经创建好了。HABTM只会引用它们。因此,您需要将记录添加到您的
提及
集合中:

#app/controllers/comments_controller.rb
Class CommentsController < ActiveRecord::Base
     def create
         @comment = Comment.new(comments_params)
         @comment.save_with_mentions
     end
end

#app/models/comment.rb
Class Comment < ActiveRecord::Base
    def save_with_mentions
        comment = self.save

        #do something to find mentioned users
        mentioned_users = User.where("user_id = 1") #example

        for user in mentioned_users do
            comment.mentions << user
        end
    end
end
#app/controllers/comments_controller.rb
类CommentsControllercomment.references可以这样建立协会:

#app/models/user.rb
Class User < ActiveRecord::Base
    has_many :comments
    has_and_belongs_to_many :mentions, join_table: "comments_users", association_foriegn_key: "comment_id"
end

Class Comment < ActiveRecord::Base
    belongs_to :author, class_name: "User", foreign_key: "author_id"
    has_and_belongs_to_many :mentions, join_table: "comments_users", foreign_key: "comment_id"
end

#comments_users
comment_id | user_id

更新

HABTM仍然需要一个表(),但区别在于它不需要主键列(只需
comment\u id | user\u id

我们已经创建了一个“自引用”的habtm关系,这意味着您不需要“创建”任何记录——它们都应该已经创建好了。HABTM只会引用它们。因此,您需要将记录添加到您的
提及
集合中:

#app/controllers/comments_controller.rb
Class CommentsController < ActiveRecord::Base
     def create
         @comment = Comment.new(comments_params)
         @comment.save_with_mentions
     end
end

#app/models/comment.rb
Class Comment < ActiveRecord::Base
    def save_with_mentions
        comment = self.save

        #do something to find mentioned users
        mentioned_users = User.where("user_id = 1") #example

        for user in mentioned_users do
            comment.mentions << user
        end
    end
end
#app/controllers/comments_controller.rb
类CommentsControllercomment.intriends完成任何给定任务总是有很多方法,但我猜您正在为您的模型和关联寻找类似的方法

用户:

用户模型关联看起来是正确的

评论:

belongs_to :users, class_name: 'User', foreign_key: 'author_id'
has_many :users
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
has_many :users
注意,所属对象应引用单一命名样式的模型(即:用户与用户)。我想你会想做一个像comment.author这样的引用来找到你评论的作者。更典型的做法是在引用用户模型时提供一个user\u id的外键,以保持清晰,但随后提供一个澄清的关联名称,如“author”或“creator”等,以供参考,如上文所示。因此,您的Comments表将具有user\u id的外键,以引用回Users表。该用户将在Rails中以“author”的名称引用

问题的第二部分与跟踪模型中的其他用户引用有关,听起来像是CommentUsers表中的一对多。所以,这听起来像是一种选择。与您的“作者”评论类似,您可能希望提供一个更清晰的名称,如“标签”,它可以只是用户的参考。
如果您计划在应用程序的其他地方使用此原则(如在照片或帖子等其他元素中引用/标记人),则此功能的另一个好选项可能是设置多态表(本质上是一个灵活的联接表)。它可以为添加特性和跟踪这些用户引用提供更大的灵活性。多态表可以有任何名称,但通常有一个“-able”类型名称,如“taggable”。这里有一个有用的参考资料:

完成任何给定任务总是有很多方法,但我猜您正在为您的模型和关联寻找类似的方法

用户:

用户模型关联看起来是正确的

评论:

belongs_to :users, class_name: 'User', foreign_key: 'author_id'
has_many :users
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
has_many :users
注意,所属对象应引用单一命名样式的模型(即:用户与用户)。我想你会想做一个像comment.author这样的引用来找到你评论的作者。更典型的做法是在引用用户模型时提供一个user\u id的外键,以保持清晰,但随后提供一个澄清的关联名称,如“author”或“creator”等,以供参考,如上文所示。因此,您的Comments表将具有user\u id的外键,以引用回Users表。该用户将在Rails中以“author”的名称引用

问题的第二部分与跟踪模型中的其他用户引用有关,听起来像是CommentUsers表中的一对多。所以,这听起来像是一种选择。与您的“作者”评论类似,您可能希望提供一个更清晰的名称,如“标签”,它可以只是用户的参考。
如果您计划在应用程序的其他地方使用此原则(如在照片或帖子等其他元素中引用/标记人),则此功能的另一个好选项可能是设置多态表(本质上是一个灵活的联接表)。它可以为添加特性和跟踪这些用户引用提供更大的灵活性。多态表可以有任何名称,但通常有一个“-able”类型名称,如“taggable”。这里有一个有用的参考:是的!这是正确的方法。谢谢@Pavan。那我就同意了。要从注释中获取作者,我可以说comment.author,然后获取其他用户,是comment.users,还是将作者包括在查询中?
comment.author
?你给你的作者下定义了吗