Question

I have method in my model

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

I would like to rewrite that in JBuilder and also add another attribute: type_name: self.class.name

Was it helpful?

Solution

My answer:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top