Гайз, нид хелп)
Есть три модели
class StoreItem < ApplicationRecord
has_many :store_items_stores, dependent: :destroy
has_many :stores, through: :store_items_stores
end
class StoreItemsStore < ApplicationRecord
self.primary_key = :store_id
belongs_to :store
belongs_to :store_item
end
class Store < ApplicationRecord
has_many :store_items_stores, dependent: :destroy
has_many :store_items, through: :store_items_stores
end
Есть два айтема:
"store_item": {
"id": 1,
Со связями:
"store_items_store": [
{
"store_id": 1,
"store_item_id": 1,
},
{
"store_id": 2,
"store_item_id": 1,
},
{
"store_id": 3,
"store_item_id": 1,
}
]
}
{
"store_item": {
"id": 2,
"store_items_store": [
{
"store_id": 1,
"store_item_id": 2,
},
{
"store_id": 2,
"store_item_id": 2,
},
{
"store_id": 3,
"store_item_id": 2,
}
Мне нужно удалить один из них, вместе с ЕГО связями.
Когда я вызываю StoreItem.last.destroy
у первого айтема пропадают связи:
"store_item": {
"id": 1,
"store_items_store": [ ]
Логи:
StoreItem Load (2.1ms) SELECT store_items
.* FROM store_items
WHERE (flyer_available_to <= '2020-01-17 10:29:36.360462' OR promo_end_date <= '2020-01-17 10:29:36.360515') ORDER BY store_items
.id
ASC LIMIT 1
(1.0ms) BEGIN
StoreItemsStore Load (3.8ms) SELECT store_items_stores
.* FROM store_items_stores
WHERE store_items_stores
.store_item_id
= 2
StoreItemsStore Destroy (0.6ms) DELETE FROM store_items_stores
WHERE store_items_stores
.store_id
= 1
StoreItemsStore Destroy (0.4ms) DELETE FROM store_items_stores
WHERE store_items_stores
.store_id
= 2
StoreItemsStore Destroy (0.3ms) DELETE FROM store_items_stores
WHERE store_items_stores
.store_id
= 3
StoreItem Destroy (0.6ms) DELETE FROM store_items
WHERE store_items
.id
= 2
А мне нужно, что бы destroy отрабатывал так:
DELETE FROM store_items_stores
WHERE store_items_stores
.store_id
= 1 AND store_items_stores
.store_item_id
= 2
Что я не так насетапил?