0

ユーザー認証に Devise を使用していますが、関連付けられたプロファイルをユーザーと共に破棄したいと考えています。

私の失敗した仕様は次のようになります。

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

私のユーザーモデルでは:

has_one :profile, dependent: :destroy

私のプロファイルモデルでは:

belongs_to :user

コンソールでは、次のように問題を再現できます。

2.0.0p247 :001 > @user = FactoryGirl.create(:user)
   (1.5ms)  BEGIN
  User Exists (2.9ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'person946979@example.com' LIMIT 1
  User Exists (1.7ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('person946979@example.com') 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", "person946979@example.com"], ["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: "person946979@example.com", 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: "person946979@example.com", 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 :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

ここで何が起こっているのですか?

4

3 に答える 3

1

これはどう?

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

@user.destroy の後、nil と予想される変数「profile」が変更されませんでした。おもう...

于 2013-08-16T02:38:37.297 に答える