Problems for add the watchers in a join_table

39 Views Asked by At

When I create a post, I'm adding the watchers of the post. So when the admin creates a post it will be add on the join_table post_watchers. And when I add this with the after_create I'm facing problems because it's adding the user two times. Why this is ocurring?

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

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

As you can see I used the pry for debugging when creates the post and when it shows.

The results debugging:

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: "[email protected]", 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: "[email protected]", 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: "[email protected]", created_at: "2018-03-28 14:37:28", updated_at: "2018-04-28 15:09:25", admin: true, archived_at: nil>]

So why it's adding 2 times the same user when save the post?

1

There are 1 best solutions below

0
On

After trying a lot of ways and why occur this. I notice that the

after_create :author_watches_me

I put before the

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

So I put after this has_belongs_to_many.... And know work's properly, adds just one author once.