Lechucico Lechucico -4 years ago 102
Ruby Question

Ruby for loop 2 lists

I have a ruby on rails aplication with a model called Serie. A Serie has multiple Seasons and a season has multiple Chapters.

This is the view controller for Serie.

@series = Serie.all


The main problem is that I want to calculate the numbers of chapters that contains a Serie.

Example: A serie has 2 seasons with 4 chapters each, a serie has 8 chapters with 1€ each chapter = a serie costs 8€.

I have to count it for each serie in the list and I have to pass it to the view.

On the view I use the following <% @series.each do |serie| %> If i have 2 array, 1 with series and another one with the number of chapters, how I can loop it?

Answer Source
class Season < ActiveRecord
  has_many :chapters
end

class Serie < ActiveRecord
  has_many :seasons
  has_many :chapters, through: :seasons
end

You can now do...

my_serie.chapters.count
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download