As an example for the circular dependent: :destroy
issue:
class User < ActiveRecord::Base
has_one: :staff, dependent: :destroy
end
class Staff
I faced this issue as well, and came up with a solution that isn't pretty but works. Essentially, you'd just use a destroy_user
that's similar to destroy_staff
.
class User < ActiveRecord::Base
has_one: :staff
after_destroy :destroy_staff
def destroy_staff
staff.destroy if staff && !staff.destroyed?
end
end
class Staff < ActiveRecord::Base
belongs_to :user
after_destroy :destroy_user
def destroy_user
user.destroy if user && !user.destroyed?
end
end