I have models like this:
class User < ActiveRecord::Base has_one :user_preference, dependent: :destroy end
class UserPreference < ActiveRecord::Base belongs_to :user end
``
It works fine when I call this:
(0.3ms) BEGIN
User Load (0.9ms) SELECT “users”.* FROM “users” WHERE “users”.“deleted_at” IS NULL AND “users”.“id” = $1 LIMIT 1 [[“id”, “055df158-4b3f-43f0-b73a-0c14e0ba7723”]]
UserPreference Exists (0.7ms) SELECT 1 AS one FROM “user_preferences” WHERE “user_preferences”.“user_id” = ‘055df158-4b3f-43f0-b73a-0c14e0ba7723’ LIMIT 1
SQL (0.5ms) INSERT INTO “user_preferences” (“created_at”, “updated_at”, “user_id”) VALUES ($1, $2, $3) RETURNING “id” [[“created_at”, “2014-12-08 08:45:57.630670”], [“updated_at”, “2014-12-08 08:45:57.630670”], [“user_id”, “055df158-4b3f-43f0-b73a-0c14e0ba7723”]]
(1.5ms) COMMIT
(0.2ms) BEGIN
(0.2ms) COMMIT
$ user.create_user_preference
``
But, if I call that method again, it will delete the profile
record.
$ user.create_user_preference
(0.3ms) BEGIN User Load (0.8ms) SELECT “users”.* FROM “users” WHERE “users”.“deleted_at” IS NULL AND “users”.“id” = $1 LIMIT 1 [[“id”, “055df158-4b3f-43f0-b73a-0c14e0ba7723”]] UserPreference Exists (0.7ms) SELECT 1 AS one FROM “user_preferences” WHERE “user_preferences”.“user_id” = ‘055df158-4b3f-43f0-b73a-0c14e0ba7723’ LIMIT 1 (0.3ms) ROLLBACK (0.2ms) BEGIN SQL (0.4ms) DELETE FROM “user_preferences” WHERE “user_preferences”.“id” = $1 [[“id”, “9eb6cabd-7c50-4f2b-8562-1159e1c2f4b4”]] (37.0ms) COMMIT
``
It looks weird for me. Is it a normal behaviour, or am I doing something wrong??