after_create bug or am I missing something?


I'm currently building an app that has these models:

class Project < ActiveRecord::Base
  has_many :collabs
  has_many :users, :through => :collabs

class User < ActiveRecord::Base
  has_many :collabs
  has_many :projects, :through => :collabs

# collabs is the "join" model only...

class Invitation < ActiveRecord::Base
  belongs_to :project

Then in my UsersController#new I check if there is an invitation code,
if so then I pass a hidden field to the form with the invitation code
so when the user is saved it's added to the project which was invited
to. I have this on my user model like this:

class User < ActiveRecord::Base
  attr_accessor :invitation_token
  after_create :add_to_project_by_invitation, :if => lambda { |u| !
u.invitation_token.blank? }

  def add_to_project_by_invitation
    invitation = Invitation.find_by_token(invitation_token)
    projects << invitation.project if invitation && !projects.include?

When I invite a user to the project 1 and he tries to signup with the
invitation token, then this error raises:

Mysql::Error: Duplicate entry '1-3' for key 2: INSERT INTO `collabs`
(`created_at`, `project_id`, `updated_at`, `admin`, `user_id`) VALUES
('2009-01-11 04:15:13', 1, '2009-01-11 04:15:13', 0, 3)

BUT!, when I just change the after_create callback in my User model to
before_create it goes all sweetie...

I'm using Rails 2.2.2
Could this be a bug?... The full stack is here:


Out of curiosity, where is the has_many :projects line in user.rb
relative to the after_save?
At first glance, I would guess that the has_many comes later, which is
could cause this problem -
add_to_project_if_invitation is adding a record to projects, which the
code generated by
add_multiple_associated_save_callbacks (in association.rb) is trying
to save again.

--Matt Jones

Oh, I see...

Yes, you are right. The relationshiop was later than the callback. If
I move it upper then it works all good.

Ok, thank you very much for the answer :slight_smile: