Debugging 在join_表中添加观察程序的问题
当我创建帖子时,我正在添加帖子的观察者。因此,当管理员创建一个post时,它将被添加到join_表post_watchers中。 当我用after_create添加这个时,我面临着问题,因为它添加了两次用户。这是为什么Debugging 在join_表中添加观察程序的问题,debugging,ruby-on-rails-4,pry-rails,Debugging,Ruby On Rails 4,Pry Rails,当我创建帖子时,我正在添加帖子的观察者。因此,当管理员创建一个post时,它将被添加到join_表post_watchers中。 当我用after_create添加这个时,我面临着问题,因为它添加了两次用户。这是为什么 class Post < ActiveRecord::Base after_create :author_watches_me ... has_and_belongs_to_many :watchers, join_table: "post_watchers", cla
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_控制器
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
class PostsController < ApplicationController
def show
authorize @post, :show?
@review = @post.reviews.build(state_id: @post.state_id)
end
后置控制器
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
class PostsController < ApplicationController
def show
authorize @post, :show?
@review = @post.reviews.build(state_id: @post.state_id)
end
正如您所看到的,我在创建post和显示post时使用了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>]
那么,为什么在保存帖子时会添加两倍于同一用户呢?在尝试了很多方法之后,为什么会出现这种情况。 我注意到 创建后:作者监视我 我把它放在 has_和_besive_属于_many:观察者,join_table:post_观察者,class_name:User,uniq:true 所以我放在这之后有很多。。。。只有一位作者补充道:“而且要知道作品的正确性。”