has_one,dependent:销毁不起作用



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

这是怎么回事?

您的模型看起来不错。试试这样的东西:

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

正如Heungju所说,当对应于profile的数据库行被销毁时,变量本身并没有。

这个怎么样?

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

在@user.destroy之后,"profile"(应为零的变量)没有更改。我想。。。

像这样重写我的规范可以满足我的需要:

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

相关内容

  • 没有找到相关文章

最新更新