2013-02-02 50 views
0

我有方法在我的模型如何在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

我的回答:

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