has_one,dependent:destroy not working

时间:2013-08-16 01:23:39

标签: ruby-on-rails devise destroy has-one dependent-destroy

我正在使用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

这里发生了什么?

3 个答案:

答案 0 :(得分:3)

你的模特看起来很好。尝试这样的事情:

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

像Heungju所说,虽然与profile对应的数据库行正在被销毁,但变量本身却没有。

答案 1 :(得分:1)

这个怎么样?

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

在@ user.destroy之后,预期为nil的'profile'变量未被更改。我想......

答案 2 :(得分:0)

重写我的规范就像我需要的那样:

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