My problem is really weird, I'm trying to delete a property in my API, I can already create and edit it using thunder client but it generates this error when trying to delete:
I put the dependent: destroy on the models so it should delete no matter if there is an address already created, besides I don't suppose to have an address.property_id
column because it doesn't exist, I have a relation from address to properties and another one from properties to users. when I try to delete a user that has an address and a property it generates the same error so I really don't know what it can be.
This is my schema:
enable_extension "plpgsql"
create_table "addresses", force: :cascade do |t|
t.string "street", null: false
t.integer "number", null: false
t.string "city", null: false
t.string "country", null: false
t.string "zip_code", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "blocked_periods", force: :cascade do |t|
t.date "start_date", null: false
t.date "end_date", null: false
t.bigint "property_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["property_id"], name: "index_blocked_periods_on_property_id"
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "hostings", force: :cascade do |t|
t.integer "cycle", null: false
t.integer "minimum_cycle_amount", null: false
t.float "rate", null: false
t.boolean "public", default: true, null: false
t.float "cleaning_fee"
t.bigint "user_id", null: false
t.bigint "properties_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["properties_id"], name: "index_hostings_on_properties_id"
t.index ["user_id"], name: "index_hostings_on_user_id"
end
create_table "properties", force: :cascade do |t|
t.string "name", null: false
t.string "description", null: false
t.integer "guest_capacity", null: false
t.integer "bedrooms", null: false
t.integer "beds", null: false
t.integer "bathrooms", null: false
t.integer "kind", default: 0, null: false
t.float "size", null: false
t.bigint "user_id", null: false
t.bigint "address_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["address_id"], name: "index_properties_on_address_id"
t.index ["user_id"], name: "index_properties_on_user_id"
end
create_table "property_categories", force: :cascade do |t|
t.bigint "property_id", null: false
t.bigint "category_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["category_id"], name: "index_property_categories_on_category_id"
t.index ["property_id"], name: "index_property_categories_on_property_id"
end
create_table "property_images", force: :cascade do |t|
t.string "source", null: false
t.bigint "property_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["property_id"], name: "index_property_images_on_property_id"
end
create_table "reservations", force: :cascade do |t|
t.integer "quests", null: false
t.date "check_in", null: false
t.date "check_out", null: false
t.float "price", null: false
t.bigint "user_id", null: false
t.bigint "hosting_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["hosting_id"], name: "index_reservations_on_hosting_id"
t.index ["user_id"], name: "index_reservations_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name", null: false
t.string "email", null: false
t.string "password_digest", null: false
t.integer "role", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
end
add_foreign_key "blocked_periods", "properties"
add_foreign_key "hostings", "properties", column: "properties_id"
add_foreign_key "hostings", "users"
add_foreign_key "properties", "addresses"
add_foreign_key "properties", "users"
add_foreign_key "property_categories", "categories"
add_foreign_key "property_categories", "properties"
add_foreign_key "property_images", "properties"
add_foreign_key "reservations", "hostings"
add_foreign_key "reservations", "users"
end
And my property and address model
class Property < ApplicationRecord
has_many :property_images, dependent: :destroy
has_many :property_categories, dependent: :destroy
has_many :blocked_periods, dependent: :destroy
has_one : address, dependent: : destroy
belongs_to :user
validates :name, presence: true
validates :description, presence: true
validates :guest_capacity, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
validates :bedrooms, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
validates :beds, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
validates :bathrooms, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
validates :kind, presence: true
enum kind: %i[apartment house]
validates :size, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
end
class Address < ApplicationRecord
# belongs_to :property
validates :s treet, presence: true
validates :city, presence: true
validates :country, presence: true
end
I comment in the address model that belongs to property because if I leave it I cannot create properties
And this is my User model where I have the dependent: destroy for properties and addresses
class User < ApplicationRecord
has_many :properties, dependent: :destroy
has_many :reservations, dependent: :destroy
has_secure_password
validates :name, presence: true
validates :email, presence: true, uniqueness: true
validates :password, presence: true
enum role: %i[user admin]
after_initialize :set_default_role, if: :new_record?
private
def set_default_role
self.role ||= :user
end
end
So I'm using the same logic for destroy users, properties, and addresses, I can delete an address because it doesn't have references to any table, but the property has reference to addresses and at the same time users has references to properties.