自定义了一个 json_options 如下,想返回一个树形菜单,没想到当一会再调用 children,求解
scope :children_categories, ->(parent_id) {where("parent_id = ?", parent_id)}
def temp_type_name
Cms::Category::TEMPLATE_TYPE[self.template_type]
end
def children
Cms::Category.children_categories(self.parent_id)
end
self.json_options = {
only: [:id, :name, :url, :is_page, :level, :position],
methods: [:temp_type_name, :children]
}
我最终想要实现的格式如下:
[{
"id": 13,
"name": "关于我们",
"url": "/about_us",
"position": "",
"is_page": 0,
"temp_type_name": "全页面",
"children": [{...}, {.....}]
},
{
"id": 12,
"name": "关于我们",
"url": "/cms/categories/12",
"position": "",
"is_page": 0,
"temp_type_name": "全页面",
"children": [{...}, {.....}]
}
循环的 log 如下:
.......
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
CACHE (0.0ms) SELECT `cms_categories`.* FROM `cms_categories` WHERE (parent_id = 13)
......