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

On Jan 10, 2009, at 11:24 PM, edgarjs wrote:

>
> Hi,
>
> I'm currently building an app that has these models:
>
> class Project < ActiveRecord::Base
>  has_many :collabs
>  has_many :users, :through => :collabs
> end
>
> class User < ActiveRecord::Base
>  has_many :collabs
>  has_many :projects, :through => :collabs
> end
>
> # collabs is the "join" model only...
>
> class Invitation < ActiveRecord::Base
>  belongs_to :project
> end
>
> 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?
> (invitation.project)
>  end
> end
>
> 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: http://pastie.org/357749
>
> Regards.
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to