I think that this is more appropriate for rails-core than the general list.
I’ve been doing a lot of frustrating spelunking in the new ActiveRecord code in Rails 2.1 trying to figure out why our app is not working. Here’s a simplified version of the section of our app which is causing trouble…
We’ve these classes:
class Role << ActiveRecord::Base
class Membership << ActiveRecord::Base
has_many :membership_roles, :dependent => :delete_all
has_many :roles, :through => membership_roles
membership_role = MembershipRole.create!(:membership => self, :role => role)
class MembershipRole << ActiveRecord::Base
validates_uniqueness_of :role_id, :scope => :membership_id
And there’s this observer
class MembershipObserver < ActiveRecord::Observer
Now what I’m seeing when I do something like
Membership.create!(:user => some_user, :organization => some_organization)
is that the membership observer’s after_create method gets triggered which calls add_role on the membership which creates the MembershipRole join model, which passes the validates_uniqueness validation.
So far so good,
But then after the observer notifications triggered within the processing of Membership.create!, but before the create! call returns, I’m seeing the validation being run on what looks like an unsaved copy of the MembershipRole which was created in the Membership.add_role method, and this validation fails since the just created MembershipRole with the particular membership and role ids exists in the DB.
This code all worked fine on Rails 2.0.2, something in Rails 2.1 is causing this spurious validation.
I’d welcome any ideas on how to shoot this bug.