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 8239567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:11:27+00:00 2026-06-07T20:11:27+00:00

Using Rails 3.0.10 & Ruby 1.9.2. I have a Book model which has many

  • 0

Using Rails 3.0.10 & Ruby 1.9.2.

I have a Book model which has many Pages.

class Book < ActiveRecord::Base
  has_many :pages

  # is this right?
  def pages
    Page.order('page_number asc').find_all_by_book_id(:id)
  end
end

class Page < ActiveRecord::Base
  belongs_to :book
end

When I retrieve the pages of a book, I always want them ordered by their page number. What is the proper way to handle this so that calling book.pages will return all pages in sequence?

When editing the book I also show the content of each page which can be edited. If I am using nested attributes would this return the pages in their proper sequence, assuming my previous question is resolved?

<%= form_for [@book, @pages] do |f| %>
  <%= f.fields_for :pages do |page| %>
    do something
  <% end %>
<% end %>

Thanks

  • 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-07T20:11:30+00:00Added an answer on June 7, 2026 at 8:11 pm

    I believe you could actually specify the default order directly on the association declaration:

    class Book < ActiveRecord::Base
        has_many :pages, :order => "page_number asc"
    end
    
    class Page < ActiveRecord::Base
      belongs_to :book
    end
    

    This way, you won’t have to specify the Book#pages method yourself, and it will still be ordered by page_number asc by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Ruby 1.8.6 & Rails 1.2. Models: Job , JobExtraStop : class JobExtraStop <
I'm having trouble using special characters with &-notation in ruby on rails forms. Have
I have rails app which uses Rails 3.2.6 & Ruby 1.9.3p0 version.I have number
Using Rails 3.2. I have an individual Shop post, and each shop has reviews.
I have a Ruby on Rails 3.2 app using bundler and capistrano for deployment.
At the moment I have a Ruby on Rails application which maintains my Users,
I am using the authlogic gem with Ruby on Rails, and I have been
I have seen this in a lot of code written using Ruby-on-Rails. It seems
I'm using autotest with ruby on rails. I have passing 2 passing tests when
First of all, I'm using Rails 3.0.6 and Ruby 1.9.2 I have a controller

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.