mardi 24 mars 2020

Setting a hash of instance variables to a new variable to call .each_with_index on in Ruby

I've got three instance variables:

@book, @recent_books, @popular_books

@books contains 1 currently viewed record, @recent_books contains 10 records, and @popular_books contains 10 records

I want to assign a variable to them and put them into a hash together, so in my controller I've got:

@book_list = [@book, @recent_books, @popular_books]

Now in my view I want to iterate over each of them and display the information for each:

@book_list.each_with_index do |(book_list), index|
book_list.title,
book_list.author,
index + 1,
etc

The issue I've got is this only iterates over three records instead of 21. However if I check @book_list I can see all 21 records, but @book_list.count only lists three. I understand why this is happening, but not how to fix it so that it iterates through all 21 records.

Aucun commentaire:

Enregistrer un commentaire