В моих рельсах есть следующие модели приложений.
- Аренда
- Недвижимость
- Контакт
- Адрес
class Lease < ApplicationRecord
belongs_to :estate
belongs_to :contact
end
class Contact < ApplicationRecord
has_one :address
end
lease.as_json(include: [:estate, {contact: {:include => :address}} ])
И это сгенерированный json, когда contact.address
не равен нулю:
{
id: 1,
start_date: "2019-05-09",
end_date: "2019-05-09",
title: "test title",
description: "test desc",
estate_id: 10,
contact_id: 4,
estate: {
id: 10,
address: "Address here",
lat: 37.0322021,
lng: 22.11332570000002,
},
contact: {
id: 1,
full_name: "contact name goes here",
address: {
title: 'this is the address'
}
}
}
Мне нужно не скрывать поле адресаиз json всякий раз, когда он равен нулю (это процесс по умолчанию).
Другими словами, когда contact.address
равен нулю, мне нужен следующий результат json:
{
id: 1,
start_date: "2019-05-09",
end_date: "2019-05-09",
title: "test title",
description: "test desc",
estate_id: 10,
contact_id: 4,
estate: {
id: 10,
address: "Address here",
lat: 37.0322021,
lng: 22.11332570000002,
},
contact: {
id: 1,
full_name: "contact name goes here",
address: {
title: ''
}
}
}
Уведомлениечто мне нужно поле адреса, чтобы быть там, но без значения.