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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:20:38+00:00 2026-05-28T04:20:38+00:00

Gists of controller code at bottom of page I have a simple rails nesting

  • 0

Gists of controller code at bottom of page

I have a simple rails nesting project that I’m practicing with and I’m having an issue that I can’t seem to spot.

When I visit the url: localhost:3000/authors/1/books I get ALL the books – not the books that correspond to the author with an id=1.

Here is some of my code:

I have an Author model and controller, and a Book author and controller:

class Author < ActiveRecord::Base
  has_many :books
end


class Book < ActiveRecord::Base
  belongs_to :author
end

The routes file looks like this:

resources :authors do
    resources :books    
  end

Here is the migration for Books:

class CreateBooks < ActiveRecord::Migration
  def self.up
    create_table :books do |t|
      t.string :title
      t.integer :author_id

      t.timestamps
    end
  end

  def self.down
    drop_table :books
  end
end

It contains an attribute for author_id

I also have this code in the Books _form.html.erb file:

<div class="field">
    <%= f.label :author_id %><br />
    <%= f.collection_select(:author_id, Author.all, :id, :first_name) %>
  </div>

I am able to choose an author from the drop-down box and save it successfully. The database shows that the author_id is indeed being saved to the corresponding Book model.

When I run rake routes I get this as one of the routes:

author_books GET    /authors/:author_id/books(.:format)

But, when I try this url – I just get a listing of ALL books. Not books corresponding to the proper author.

Also, I have my controllers setup to return JSON and when I put localhost:3000/authors.json it returns the proper JSON object, but when I put localhost:3000/authors/1/books.json it returns ‘null‘

Here is a screen shot of my current database listings to show that the data is definitely there:

enter image description here

Can you see anything that may be causing the problem??? I can provide more code if you need it.


EDIT

After running rails console I get the correct data – so I’m not sure why the route is still not working:

irb(main):002:0> Author.find(1).books
=> [#<Book id: 1, title: "Carrie", author_id: 1, created_at: "2012-01-08 21:20:57", updated_at: "201
2-01-08 21:20:57">]

Here is a Gist of complete routes: https://gist.github.com/1580058

Here is the Books controller: https://gist.github.com/1580064

Here is the Authors controller: https://gist.github.com/1580134

Sorry, just realized I miss-worded the original question up top – I’m getting the whole list of Books, not Authors – corrected

  • 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-05-28T04:20:39+00:00Added an answer on May 28, 2026 at 4:20 am

    You need to show us your index action in the controller. I’m guessing you have Book.all… You need

    @author = Author.includes(:books).find(params[:author_id])
    

    you also might want

    @books = @author.books
    

    Also in your show method make sure you do this

    @author = Author.find(params[:author_id])
    @book = @author.books.find(params[:id])
    

    instead of

    @book = Book.find(params[:id])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The issue is this. I have a PageViewController as my root view controller. Initialising
I need to implement a custom error page in my rails application that allows
Now that Rails 3 beta is out, I thought I'd have a look at
I understand the gist of the code, that it forms permutations; however, I was
I have gone through tons of the form_for nested resource questions and can't get
I having some trouble trying to figure out how to implement a Rails 3
Here you can see the error message along with the my application controller file
The gist of my problem is that I have a table that every even
I'm having trouble with a cocoa project. I'm displaying a keyboard composed of NSButtons,
Given the following code: JE.events = { self: this, controller: { init: function(){ $(.monthheader).click(function

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.