1

I want to loop through each item in an array, but the last variable could be an array itself:

<% [@in_force_item, @draft_item,@historical_items].compact.each do |item| %>

it seems to fail at this line:

 <td>
  <%= datetime_to_string item.updated_at %>
 </td>

Is it because the historical_items is actually an array?

1 Answer 1

3

Yes, you can't call .updated_at on the array. Just flatten the array before your iteration:

<% [@in_force_item, @draft_item,@historical_items].compact.flatten.each do |item| %>
Sign up to request clarification or add additional context in comments.

1 Comment

Or just splat it: [@in_force_item, @draft_item, *@historical_items].each ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.