确保数据库完整性的最佳方法是什么,这样,如果我删除列表,所有相关记录也将被删除。
汽车,卡车,公司,卖家,收藏夹?
#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