Iterating through the collections with select method?

Hi all,

I’m building a site based on the middleman-starter-netlify-cms example on Github. The problem is that I’m getting errors when using select method on my data. Basically, I want to post only the future events on the homepage with the inline ruby below :

<%= data.events.select { |e| e[1].date.future? }%>

But I get:

NoMethodError at /
`## undefined method `date' for "n":String`

However, sort method used in the example code works pretty fine:

  <%  data.events.sort {|a,b| b[1].date <=> a[1].date}.first(3).each do | event | %>
  <%= partial 'partials/events/event', locals: {event: event[1]} %>
  <% end %> 

I thought select works with any enumerable, hash or array. What am I missing here?

Here is my event collection on the config.yml

collections:

  - name: event
    label: Event
    format: yml
    extension: yml
    folder: data/events/
    create: true
    slug: '{{title}}'
    fields:
      - {label: Title, name: title, widget: string, required: true}
      - {label: Publish Date, name: date, widget: datetime}
      - {label: Image, name: image, widget: image, required: true}
      - {label: Place, name: place, widget: string, required: true}
      - {label: Time, name: time, widget: datetime, date_format: "DD.MM.YYYY", time_format: "HH:mm", required: true}
      - {label: Body, name: body, widget: markdown }

Hi @ozgurozan,

I don’t know this particular template you’re using, but the NoMethodError suggests that your event object e[1] does not have attribute (method) date – it seems to be a simple String. Of course sort method works for String, so the second example works fine. Maybe try to output e[1].inspect and e.inspect to your HTML and see what’s there? Are other fields from your collection definition work fine?

1 Like