Skip to content

Callbacks definitions and HABTM association are order dependent #8672

Closed
killergti opened this Issue Jan 1, 2013 · 8 comments

3 participants

@killergti

I'm using rails 3.1.0
I am observing the same issue, as described here: http://ryanbigg.com/2010/04/has_and_belongs_to_many-double-insert/

class Ticket < ActiveRecord::Base
after_create :creator_watches_me
has_and_belongs_to_many :watchers, :join_table => "ticket_watchers",
    :class_name => "User"
private
  def creator_watches_me
    self.watchers << user
  end
end

So when i insert a new ticket, here what it shows:

  SQL (21.6ms)  INSERT INTO "tickets" ("created_at", "description", "project_id", "state_id", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?)  [["created_at", Tue, 01 Jan 2013 13:32:58 UTC +00:00], ["description", "assdklsdj ;lksdjglksdjgsdjpgsdgd \r\n"], ["project_id", 2], ["state_id", nil], ["title", "Implement delete tags functionality"], ["updated_at", Tue, 01 Jan 2013 13:32:58 UTC +00:00], ["user_id", 2]]
  User Load (0.1ms)  SELECT "users".* FROM "users" INNER JOIN "ticket_watchers" ON "users"."id" = "ticket_watchers"."user_id" WHERE "ticket_watchers"."ticket_id" = 6
   (0.1ms)  INSERT INTO "ticket_watchers" ("ticket_id", "user_id") VALUES (6, 2)
   (0.0ms)  INSERT INTO "ticket_watchers" ("ticket_id", "user_id") VALUES (6, 2)

Also i could add, that i have an observer, registered in this Ticket class.

@senny
Ruby on Rails member
senny commented Jan 1, 2013

Please use "```" around your code and log blocks. This tells github to format it nicely. (I edited your message).

@senny
Ruby on Rails member
senny commented Jan 1, 2013

What is the purpose behind creator_watches_me? As I see it the line self.watchers << user inserts your second record. If you remove the after_create callback it should only insert one.

@killergti

Actually, this callback adds an author of a ticket to a list of watchers. So this is the only place where this association ever changes. More over, this code was just copy pasted from the "Rails 3 in Action" book (Chapter 12).

I tried to remove this callback. So the ticket_watchers table doesn't update. Then i tried to change the method from "<<" to "=", but this didn't help too. Also I am pretty sure that this callback invokes only once.

Thanks for the update

@senny
Ruby on Rails member
senny commented Jan 1, 2013

If you could upload a small sample application on github it would facilitate debugging a lot.

@killergti

I've just root caused the "issue". It happens because my callback declaration sits before association declaration. I'm newbie in ruby/rails, so may be it is an expected behavior.

Here is the working version.

class Ticket < ActiveRecord::Base


  validates :title, :presence => true
  validates :description, :presence => true, :length => {:minimum => 10}



  searcher do 
    label :tag, :from => :tags, :field => "name"
    label :state, :from => :state, :field => "name"
  end

  belongs_to :project
  belongs_to :user
  belongs_to :state

  has_many :assets
  accepts_nested_attributes_for :assets
  has_many :comments

  has_and_belongs_to_many :tags
  has_and_belongs_to_many :watchers, :join_table => "ticket_watchers", :class_name => "User"

  after_create :creator_watches_me

  def tag!(tags)
    if tags
      tags = tags.split(" ").map do |tag|
        Tag.find_or_create_by_name(tag)
      end
      self.tags << tags
    end
  end

private
  def creator_watches_me
    self.watchers << user
  end
end

Sorry for spending your time

@senny
Ruby on Rails member
senny commented Jan 1, 2013

@killergti thanks! I'll dig into the source to find the cause for this. I don't think the order should be relevant. I'll report back when I know more.

@killergti killergti added the stale label May 27, 2014
@rails-bot

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this Aug 19, 2014
@rails-bot

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.