/ / JBuilder में in_json का निर्माण कैसे करें? - रूबी-ऑन-रेल, रूबी, जसन, जूलरी

JBuilder में to_json का निर्माण कैसे करें? - रूबी-ऑन-रेल, रूबी, जेसन, जेबिल्डर

मेरे मॉडल में विधि है

def indexed_json
to_json(
only: [:id, :chanell_id, :title],
include: [
:locations,
{categories: {only: [:name, :id]}}
]
)
end

मैं JBuilder में फिर से लिखना चाहता हूं और एक अन्य विशेषता भी जोड़ना चाहता हूं: type_name: self.class.name

उत्तर:

जवाब के लिए 0 № 1

मेरा जवाब:

def to_indexed_json
Jbuilder.encode do |json|
json.id self.id
json.app_id self.app_id
json.title self.title
json.type_name self.class.name.titleize
json.app_name self.app.name
json.locations self.locations
json.categories self.categories, :name, :id
end
end