Indexing for hiearchicalMenu

Hello, With my rails-react e-shop app, I am trying to index my items.categories in the proper format. I am having some trouble. Could you please give me hand.

I am able to get the category of the item :

add_attribute :get_category
def get_category
return category.name
end

Getting as result in Algolia:

get_category:Choux

I am able to get the parent category of the item (as an object) :

add_attribute :grandparent
def grandparent
Category.where(id: category.parent_id)
end

Getting as result in Algolia:

grandparent: [{
id: 169
name: Légumes
description:

}]

I am not able to get the grandparent name:

add_attribute :grandparent_name
def grandparent_name
grandparent = Category.where(id: category.parent_id).first.name
end

Getting as result in Algolia:

grandparent_name: Category

How can I get the expected result :

{
“categories.lvl0”: “légumes”,
“categories.lvl1”: “légumes > Choux”,

}

Thank you for your help

ok, I finaly could make it by using

Category.find(cat_id)