当我创建帖子时,我添加了帖子的观察者。因此,当管理员创建帖子时,它将添加到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倍?
答案 0 :(得分:0)
尝试了很多方法后,为什么会这样。 我注意到了
has_and_belongs_to_many :watchers, join_table: "post_watchers", class_name: "User", uniq: true
我把它放在
之前 $(this).get(0).files[0].size
。
所以我把它放在了_beongs_to_many之后......并且知道工作正常,只添加一位作者。