ruby on rails - has_one, dependent: destroy not working -
i using devise user authentication , destroy associated profile along user.
my failing spec looks this:
it "should destroy associated profile" profile = @user.profile @user.destroy expect(profile).to be_nil end
and
in user model:
has_one :profile, dependent: :destroy
in profile model:
belongs_to :user
in console, can reproduce issue this:
2.0.0p247 :001 > @user = factorygirl.create(:user) (1.5ms) begin user exists (2.9ms) select 1 one "users" "users"."email" = 'person946979@example.com' limit 1 user exists (1.7ms) select 1 one "users" lower("users"."email") = lower('person946979@example.com') limit 1 sql (15.7ms) insert "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 "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".* "profiles" "profiles"."user_id" = $1 order "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 "profiles" "profiles"."id" = $1 [["id", 4]] sql (5.4ms) delete "users" "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">
interestingly, user appears have been deleted.
2.0.0p247 :003 > @user.reload.destroy user load (2.7ms) select "users".* "users" "users"."id" = $1 limit 1 [["id", 25]] activerecord::recordnotfound: couldn't find user id=25
what going on here?
your model seems fine. try this:
it "should destroy associated profile" profile = @user.profile @user.destroy expect(profile.find(profile.id)).to be_nil end
like heungju said, while database row corresponds profile
being destroyed, variable isn't.
Comments
Post a Comment