I have a Stories with a has_many relationship to chapters
Chapter has_many relationship with pages
I would like to return a list of pages on my Stories object with a method Stories.pages
def pages
self.chapters.map do |c| c.pages end
end
This is not returning a list of lists
ive resorted to doing this
def pages
pages =[]
self.chapters.each do |c|
c.pages.each do |p|
pages << p
end
end
end
I’m new to ruby, with php and c# background, i know i could do this with an association directly between stories and pages or create custom query (INNER JOIN).
But would like to wrap my head around map, reduce methods a little.
No need to create your own custom method, Rails already provides this as
has_many :throughassociations: