2013-08-16 2 views
0

J'utilise Devise pour mon authentification d'utilisateur et je voudrais détruire un profil associé avec l'utilisateur.has_one, dependent: destroy ne fonctionne pas

Ma spec défaut ressemble à ceci:

it "should destroy associated profile" do 
    profile = @user.profile 
    @user.destroy 
    expect(profile).to be_nil 
end 

Et

Dans mon modèle d'utilisateur:

has_one :profile, dependent: :destroy 

Dans mon modèle de profil:

belongs_to :user 

Dans la console , Je peux reproduire le émettre comme ceci:

2.0.0p247 :001 > @user = FactoryGirl.create(:user) 
    (1.5ms) BEGIN 
    User Exists (2.9ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    User Exists (1.7ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
    SQL (15.7ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["email", "[email protected]"], ["encrypted_password", "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5z3G77Dgja"], ["name", "Example User"], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00]] 
    SQL (3.8ms) INSERT INTO "profiles" ("created_at", "updated_at", "user_id") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["user_id", 25]] 
    Profile Load (3.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1 [["user_id", 25]] 
    (2.2ms) COMMIT 
=> #<User id: 25, email: "[email protected]", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User"> 
2.0.0p247 :002 > @user.destroy 
    (1.0ms) BEGIN 
    SQL (2.5ms) DELETE FROM "profiles" WHERE "profiles"."id" = $1 [["id", 4]] 
    SQL (5.4ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 25]] 
    (2.0ms) COMMIT 
=> #<User id: 25, email: "[email protected]", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User"> 

Fait intéressant, l'utilisateur semble avoir été effacé.

2.0.0p247 :003 > @user.reload.destroy 
    User Load (2.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 25]] 
ActiveRecord::RecordNotFound: Couldn't find User with id=25 

Que se passe-t-il ici?

Répondre

0

Réécriture ma spec comme ça fait ce que je dois:

it "should destroy associated profile" do 
     expect { 
     @user.destroy 
     }.to change(Profile, :count).by(-1) 
    end 
1

Que pensez-vous de cela?

it "should destroy associated profile" do 
    profile = @user.profile 
    @user.destroy 
    expect(@user.profile).to be_nil 
end 

Après @ user.destroy, le 'profile', variable qui devait être nulle, n'a pas été modifié. Je pense ...

3

Votre modèle semble bien. Essayez quelque chose comme ceci:

it "should destroy associated profile" do 
    profile = @user.profile 
    @user.destroy 
    expect(Profile.find(profile.id)).to be_nil 
end 

Comme Heungju dit, alors que la ligne de base de données qui correspond à profile est détruite, la variable elle-même est pas.

+0

Merci, cela est en effet la question que je courais dans. La requête de base de données comme ceci a renvoyé une erreur plutôt que nulle, ainsi j'ai réécrit la spécification. Je soumets cette réécriture comme réponse. – mpgarate

Questions connexes