Creating a product with warehouse_x (foreign key to Warehouse table) that is outside default_scope i.e. warehouse_x has warehouse_type **damage**.
Unable to create record and throwing error.
ActiveRecord::RecordInvalid: Validation failed: Warehouse must exist
Schema
create_table "product", force: :cascade do |t|
t.float "unit_price"
t.bigint "warehouse_id", null: false
...
t.index ["warehouse_id"], name: "index_stock_details_on_warehouse_id"
end
create_table "warehouses", force: :cascade do |t|
t.string "warehouse_name"
...
t.integer "warehouse_type", default: 0
end
Warehouse model (warehouse.rb)
class Warehouse < ApplicationRecord
has_many :products
default_scope {where(warehouse_type: :ok_product)}
scope :damaged, -> {unscoped.where(warehouse_type: :damage)}
enum warehouse_type: {
ok_product: 0,
damage_product: 2
}
end
Product model (product.rb)
class Product < ApplicationRecord
belongs_to :warehouse
end
How can I create a record with foreign key outside default (relation table).
Is validation generated by your belongs_to association, you can disable like this: