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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:05:52+00:00 2026-06-01T14:05:52+00:00

Associations: location has_many :comments comment belongs_to :location For some reason, this GET: /locations/5/comments.json is

  • 0

Associations:

location  has_many :comments
comment  belongs_to :location

For some reason, this GET:

/locations/5/comments.json 

is acting like this GET:

/comments.json

Started GET "/locations/5/comments.json" for 127.0.0.1 at 2012-04-10 21:18:00 -0700
  Processing by CommentsController#index as JSON
  Parameters: {"location_id"=>"5"}
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" 
Completed 200 OK in 21ms (Views: 1.0ms | ActiveRecord: 0.7ms)

Note the SQL query: SELECT “comments”.* FROM “comments”

The route is set up like this:

  resources :locations do
    resources :comments
  end

Rake routes confirms the route:

location_comments GET    /locations/:location_id/comments(.:format)          {:action=>"index", :controller=>"comments"}

Here is the index action:

 def index
    @comments = Comment.all
    respond_to do |format|
      format.json { render json: @comments }
    end
  end

Is this the right action? It is consistent with the result, but I am not sure what else should be here. I’ve never had a problem with nested resources before, so I’ve never looked into the details.

  • 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-01T14:05:53+00:00Added an answer on June 1, 2026 at 2:05 pm

    try this:

    def index
      @location = Location.find(params[:location_id])
      @comments = @location.comments
      respond_to do |format|
        format.json { render json: @comments }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so my associations are: Outlet -> has_many :monitorings Monitoring -> belongs_to :outlet My
To start, I have 3 Models: Location has_many :products User has_many :products Products belongs_to
Existing associations class Job < ActiveRecord::Base belongs_to :client belongs_to :contact belongs_to :location has_one :geofence,
I have a function like : # get all locations, if the user has
This question Check all associations before destroy in rails suggests using :dependent => :restrict
I'm trying to fetch associations of a model dynamically. For ex; model1 has_many model2s
I have setup some simple associations in my rails app: resources :properties do resources
Currently, an Item belongs_to a Company and has_many ItemVariants . I'm trying to use
I have a has_many and belongs_to association set up between two models: Project and
This is a pretty frustrating newb question, but I can't seem to get iteration

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.