首页 > 解决方案 > 在 join_table 中添加观察者的问题

问题描述

当我创建帖子时,我正在添加帖子的观察者。因此,当管理员创建帖子时,它将添加到 join_table post_watchers 中。当我用 after_create 添加它时,我遇到了问题,因为它添加了两次用户。为什么会出现这种情况?

class Post < ActiveRecord::Base
  after_create :author_watches_me
...
has_and_belongs_to_many :watchers, join_table: "post_watchers", class_name: "User", uniq: true
...
private

    def author_watches_me
      if self.author.present? && !self.watchers.include?(self.author)
        self.watchers << self.author
        binding.pry
      end
    end

管理员::post_controller

class Admin::PostsController < Admin::ApplicationController
   def create
    @post = Post.new(post_params)
    @post.author = current_user

    if @post.save
      flash[:notice] = "Post has been created."
      binding.pry
      redirect_to @post
    else
      flash[:alert] = "Post has not been created."
      render "new"
    end
  end

post_controller

class PostsController < ApplicationController
   def show
    authorize @post, :show?
    @review = @post.reviews.build(state_id: @post.state_id)
  end

如您所见,我在创建帖子和显示帖子时使用了 pry 进行调试。

调试结果:

86: def author_watches_me
    87:   if self.author.present? && !self.watchers.include?(self.author)
    88:     self.watchers << self.author
 => 89:     binding.pry
    90:   end
    91: end

[1] pry(#<Post>)> Post.last.watchers
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts"  ORDER BY "posts"."id" DESC LIMIT 1
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "post_watchers" ON "users"."id" = "post_watchers"."user_id" WHERE "post_watchers"."post_id" = ?  [["post_id", 29]]
=> [#<User id: 1, email: "admin@newscity.com", created_at: "2018-03-28 14:37:28", updated_at: "2018-04-28 15:09:25", admin: true, archived_at: nil>]
[2] pry(#<Post>)>
  SQL (0.3ms)  INSERT INTO "categorizations" ("category_id", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["category_id", 1], ["post_id", 29], ["created_at", "2018-04-28 17:08:36.207670"], ["updated_at", "2018-04-28 17:08:36.207670"]]
  SQL (0.1ms)  INSERT INTO "post_watchers" ("post_id", "user_id") VALUES (?, ?)  [["post_id", 29], ["user_id", 1]]
   (2.9ms)  commit transaction

From: /Users/romenigld/ror_workspace/projects/news_city/app/controllers/admin/posts_controller.rb @ line 14 Admin::PostsController#create:

     8: def create
     9:   @post = Post.new(post_params)
    10:   @post.author = current_user
    11:
    12:   if @post.save
    13:     flash[:notice] = "Post has been created."
 => 14:     binding.pry
    15:     redirect_to @post
    16:     binding.pry
    17:   else
    18:     flash[:alert] = "Post has not been created."
    19:     render "new"
    20:   end
    21: end

[1] pry(#<Admin::PostsController>)> Post.last.watchers
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts"  ORDER BY "posts"."id" DESC LIMIT 1
  User Load (0.2ms)  SELECT "users".* FROM "users" INNER JOIN "post_watchers" ON "users"."id" = "post_watchers"."user_id" WHERE "post_watchers"."post_id" = ?  [["post_id", 29]]
=> [#<User id: 1, email: "admin@newscity.com", created_at: "2018-03-28 14:37:28", updated_at: "2018-04-28 15:09:25", admin: true, archived_at: nil>,
 #<User id: 1, email: "admin@newscity.com", created_at: "2018-03-28 14:37:28", updated_at: "2018-04-28 15:09:25", admin: true, archived_at: nil>]

那么为什么在保存帖子时会添加 2 次相同的用户呢?

标签: debuggingruby-on-rails-4pry-rails

解决方案


在尝试了很多方法后,为什么会出现这种情况。我注意到

after_create :author_watches_me

我放在前面

has_and_belongs_to_many :watchers, join_table: "post_watchers", class_name: "User", uniq: true.

所以我在这个has_belongs_to_many之后加上了......并且知道工作是正确的,只添加了一位作者。


推荐阅读