首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在join_table中添加监视器的问题

在join_table中添加监视器的问题
EN

Stack Overflow用户
提问于 2018-04-29 01:05:21
回答 1查看 25关注 0票数 0

当我创建一个帖子时,我添加了帖子的观察者。因此,当管理员创建一个帖子时,它将被添加到join_table post_watchers上。当我用after_create添加这个的时候,我会遇到问题,因为它会添加两次用户。为什么会发生这种情况?

代码语言:javascript
复制
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

admin::post_controller

代码语言:javascript
复制
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

代码语言:javascript
复制
class PostsController < ApplicationController
   def show
    authorize @post, :show?
    @review = @post.reviews.build(state_id: @post.state_id)
  end

正如您所看到的,我在创建post和显示post时使用了pry进行调试。

调试结果:

代码语言:javascript
复制
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倍的相同用户呢?

EN

回答 1

Stack Overflow用户

发布于 2018-05-04 00:09:43

在尝试了很多方法之后,为什么会发生这样的事情。我注意到

after_create :author_watches_me

我把它放在

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

所以我把这个has_belongs_to_many..。并且知道工作是正确的,只有一位作者补充道。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50079028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档