1

What is the best way to ensure database integrity so that if I delete a listing, all related records get deleted too.

cars, trucks, firms, sellers, favorites?

#Schema

ActiveRecord::Schema.define(version: 2019_09_03_145113) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "cars", force: :cascade do |t|
    t.string "style"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.index ["user_id"], name: "index_cars_on_user_id"
  end

  create_table "trucks", force: :cascade do |t|
    t.string "model"
    t.bigint "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_trucks_on_user_id"
  end

  create_table "favorites", force: :cascade do |t|
    t.integer "user_id"
    t.integer "listing_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["listing_id"], name: "index_favorites_on_listing_id"
    t.index ["user_id", "listing_id"], name: "index_favorites_on_user_id_and_listing_id", unique: true
    t.index ["user_id"], name: "index_favorites_on_user_id"
  end

  create_table "firms", force: :cascade do |t|
    t.integer "number_of_stock"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "firmable_type"
    t.bigint "firmable_id"
    t.index ["firmable_type", "firmable_id"], name: "index_firms_on_firmable_type_and_firmable_id"
  end

  create_table "sellers", force: :cascade do |t|
    t.boolean "is_selling"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "sellable_type"
    t.bigint "sellable_id"
    t.index ["sellable_type", "sellable_id"], name: "index_sellers_on_sellable_type_and_sellable_id"
  end

  create_table "listings", force: :cascade do |t|
    t.text "description"
    t.string "business_postal_code"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "listable_type"
    t.bigint "listable_id"
    t.decimal "latitude", precision: 10, scale: 6
    t.decimal "longitude", precision: 10, scale: 6
    t.text "interest_options", default: [], array: true
    t.index ["listable_type", "listable_id"], name: "index_listings_on_listable_type_and_listable_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    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 "cars", "users"
  add_foreign_key "trucks", "users"
end


#MODELS

class Car < ApplicationRecord
  belongs_to :user

  has_one  :listing, as: :listable
  has_one  :firm, as: :firmable
  has_one  :seller, as: :sellable
end

class Truck < ApplicationRecord
  belongs_to :user

  has_one  :listing, as: :listable
  has_one  :firm, as: :firmable
  has_one  :seller, as: :sellable
end

class Listing < ApplicationRecord
  belongs_to :listable, polymorphic: true
  has_many :favorites, dependent: :destroy
  has_many :users_who_favorited, through: :favorites, source: :user
end

class Firm < ApplicationRecord
  belongs_to :firmable, polymorphic: true
end

class Seller < ApplicationRecord
  belongs_to :sellable, polymorphic: true
end

class Favorite < ApplicationRecord
  belongs_to :user
  belongs_to :listing
end
tadman
  • 208,517
  • 23
  • 234
  • 262
user2012677
  • 5,465
  • 6
  • 51
  • 113
  • 1
    You need to make associations on the `Listing` model pointing to those other tables. Once you've done that, add `dependent: :destroy` to each – max pleaner Sep 06 '19 at 00:02
  • but they are not direct relationships, so would the destroy cascade through the relationships, as long as I have dependent: :destory on the other models? – user2012677 Sep 06 '19 at 00:03
  • delete-cascade-vs-dependent-destroy: https://stackoverflow.com/questions/12556614/rails-delete-cascade-vs-dependent-destroy – Christian Sep 06 '19 at 00:04
  • 1
    @user2012677 for example if you have X=>Y=>Z and you want destroying X to destroy Y and Z, you would put dependent: :destroy on the X=>Y association as well as the Y=>Z association – max pleaner Sep 06 '19 at 04:56

0 Answers0