Template doesn't show data of an array of objects

Hi all!, Im Juan Miguel, founder of a little startup from Spain, Crispfind. i hope you could help me with this problem.

I want to retrieve data from my index but I’m having problems with relationships.

Rails 5 + Algolia + Instantsearch

###Display configuration at dashboard->index->display

###My model

  algoliasearch do
    attribute :id, :name, :avatar, :relevancy, :is_blogger, :is_youtuber, :slug
    add_attribute :user_tags
    add_attribute :user_categories
    add_attribute :user_attitudes
    searchableAttributes ['user_tags','name']
  end

  def user_tags
    self.tags.select { |t| t.valid? }.map do |t|
      { name: t.name }
    end
  end

  def user_categories
    self.categories.select { |c| c.valid? }.map do |c|
      { name: c.name }
    end
  end

Data from index

I’m trying this template to access to the name of the first tag and category

But nothing happens. Only data like “name” or “avatar” are shown because they are simple variables.

Any help?

Thanks.

Hi @jmgonzalezit thanks for your message

Can you send us a link to reproducible test case using jsFiddle?

What kind of test? My rails user model works fine and indexes well like the picture of algolia dashboard shows. The problem is when I retrieve data from relationships. Data like “avatar” and “name” are shown but user_tags[0].name doesn’t.

I don’t used to test anything in “rails way” (my fault, I know), but in this case is visually proven I think.

Thanks in advance.

Hi again @Bobylito , The jsfiddle is here: https://jsfiddle.net/1g3j5q6k/4/ but didn’t work there. Any advice? Thank you very much.

Sorry for the late answer @jmgonzalezit. It took me a while to figure the problem :slight_smile: The problem is that when using mustache the syntax to fetch a single element in an array is different from JS. It uses dots at each step, so arr[0] is arr.0 in mustache.

You can find the fixed example here: https://jsfiddle.net/bobylito/dwbxqtg2/

Thank you so much. You saved my life :D. Sometimes when you are stucked little mistakes goes unnotice.

1 Like