Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:25:16+00:00 2026-06-13T06:25:16+00:00

I have a Stories with a has_many relationship to chapters Chapter has_many relationship with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:25:18+00:00Added an answer on June 13, 2026 at 6:25 am

    No need to create your own custom method, Rails already provides this as has_many :through associations:

    class Story < ActiveRecord::Base
      has_many :chapters
      has_many :pages, :through => :chapters
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Story and Chapter . A story has_many chapters, one of
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I would like to be able to list the 10 most popular tags (subjects)
I have the following relationship: store.rb -> has_many :products product.rb -> belongs_to :store routes.rb
I have the following relationship: store.rb -> has_many :products product.rb -> belongs_to :store routes.rb
I have a story model that has many posts. I list the stories in
I have heard conflicting stories on this topic and am looking for a little
I have a site where people post stories, and I want part of each
So I have been tasked with modifying the Stories Overview report for TFS. I
I have a situation where I am providing a method to query for data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.