Help with syntax - trying to filter and loop through a dataset where one of its fields (a multi-link field from datoCMS) matches a value

Need some help with syntax

<% dato.articles.select{ |t| t.topic.include?(topic.title) }.sort_by(&:date_published).each do |article| %>

I am trying to loop through all the articles WHERE the topic (which I believe is an array DatoCMS multi-link field) includes the topic value passed to page via locals during a proxy build

Can anyone help
Dave

Can you share the datasets you are trying to filter? Raw YAML or data.articles.to_json works I suppose. Any other data (like the data you are trying to filter with) is useful as well.

1 Like

Hi @tomrutgers

thanks for jumping in.
Not sure how best to share, the data is stored in DatoCMS
this is the graphQL output from the API explorer
Might also be helpful to know I am using the DATOCMS middleman gem to interact with the data

{
  "data": {
    "article": {
      "topic": [
        {
          "title": "Business"
        },
        {
          "title": "Web Design"
        },
        {
          "title": "Web Advice"
        }
      ]
    },
    "allArticles": [
      {
        "topic": [
          {
            "title": "Business"
          },
          {
            "title": "Web Design"
          },
          {
            "title": "Web Advice"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "Web Advice"
          },
          {
            "title": "Web Strategy"
          },
          {
            "title": "Business"
          },
          {
            "title": "Web Design"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "Web Advice"
          },
          {
            "title": "Web Design"
          },
          {
            "title": "Web Strategy"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "Web Advice"
          },
          {
            "title": "Web Design"
          },
          {
            "title": "Business"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "CMS"
          },
          {
            "title": "Web Advice"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "Web Advice"
          },
          {
            "title": "Web Project Management"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "eCommerce"
          },
          {
            "title": "Web Advice"
          },
          {
            "title": "Business"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "Web Design"
          },
          {
            "title": "Web Project Management"
          },
          {
            "title": "Web Development"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "Web Design"
          },
          {
            "title": "Web Advice"
          },
          {
            "title": "Web Development"
          }
        ]
      },
      {
        "topic": [
          {
            "title": "Web Advice"
          },
          {
            "title": "Web Project Management"
          }
        ]
      }
    ]
  }
}



{
  "data": {
    "allTopics": [
      {
        "title": "Web Advice"
      },
      {
        "title": "Web Design"
      },
      {
        "title": "Web Strategy"
      },
      {
        "title": "Business"
      },
      {
        "title": "SEO"
      },
      {
        "title": "Web Development"
      },
      {
        "title": "eCommerce"
      },
      {
        "title": "Logo Design"
      },
      {
        "title": "Web Project Management"
      },
      {
        "title": "CMS"
      }
    ]
  }
}

is this the kind of thing you are after?
My problem is with the correct syntax to use to filter the dataset against an array (the topics), and each article stores an array of topics
SO I want to loop through each article and look at the topic array and compare it to the topic variable within the loop

I’ll add my rb.config too that might help!

dato.tap do |dato|
dato.articles.each do |article|
  proxy "/articles/#{article.slug}/index.html", "/templates/article.html",
    locals: { article: article }
end
end

dato.tap do |dato|
dato.topics.each do |topic|
  proxy "/articles/topics/#{topic.slug}/index.html", "/templates/topics.html",
   locals: { topic: topic }
end
end

dato.tap do |dato|
  paginate dato.articles.sort_by(&:date_published), "/articles", "/templates/articles.html", per_page: 6
end

t.topic looks like an array of objects, therefore you can’t use include?. Try something like this:

<% dato.articles.select{ |t| t.topic.select { |item| item.title == topic.title } }... %>

Obviously this is a bit of a guess since I can’t debug any of it. Try logging all of your data to find the right method of selecting entries if the above doesn’t work.

Thanks Tom that looks promising. I’m not 100% sure what you mean by logging all my data. I assume this is some sort of troubleshooting approach, but not clear how to go about it

oh and FYI the above might be getting close, it returns all articles, for each topic page/proxy but before I was getting no articles returned!! lol

you can use puts data.articles to log the data to your console / terminal. Or data.articles.to_json in erb to see what your data structure looks like. If you do that for all of your data objects step by step, you might get a better view as to what is going on.

I recommend you break everything down before you start to filter your data. Something like this might help:

<%= dato.articles.to_json %>
<%= topic.to_json %>
<%= dato.articles.select{ |t| t.topic.select { |item| item.title == topic.title } }.to_json %>

Once you’re sure you’ve got the right data selected, you can start sorting and iterating etc.

1 Like

this is an example of the log for dato.articles.to_json, just for one article

\"topic\"=>[#\"Web Advice\", \"slug\"=>\"web-advice\", \"seo\"=>nil}>, #\"Web Strategy\", \"slug\"=>\"web-strategy\", \"seo\"=>nil}>, #\"Business\", \"slug\"=>\"business\", \"seo\"=>nil}>, #\"Web Design\", \"slug\"=>\"web-design\", \"seo\"=>nil}>]

and for the topic.to_json

"#\"eCommerce\", \"slug\"=>\"ecommerce\", \"seo\"=>nil}>"

its not clear to me why it uses # and not title???

and for the full select it is just returning the entire dataset in Jason which i won’t post!!

Thanks buddy I now have a way to try and troubleshoot, but can you shed any light on the use of # where i wuld expect title (the name of the field to be) to be

Honestly, I can’t really tell without taking a look myself. Feel free to send me a DM if you’re able to share your repo.

happy to Tom but not seeing option to send message when i click your avatar??
, whats your email?

This will get you the data you need:

<% dato.articles.select { | item | item.topic.find { |t| t.title == topic.title } } %>
1 Like

pretty sure I tried all of those methods somewhere along the way, just never in the right order!! lol, thanks buddy I owe you one.
Now I know how to nest methods when selecting based on an array field :wink: