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

  • Home
  • SEARCH
  • 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 6215797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:07:14+00:00 2026-05-24T07:07:14+00:00

As the official site, I defined two models Post and Comment . Comment is

  • 0

As the official site, I defined two models Post and Comment. Comment is nested in Post.

resources :posts do
  resources :comments
end

So I can use @post.comments to access all comments a post own. In console:

$ post = Post.new()
$ post.comments.class
> Array

But:

$ post.comments.respond_to?("build")
> true

Why? An array has the method build? How did Rails do? And how can I know other hidden methods of post.comments?

  • 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-24T07:07:15+00:00Added an answer on May 24, 2026 at 7:07 am

    Firstly, your resources calls are in your routes.rb file, which only deals with the URL parsing side of Rails. It’s nothing to do with the associations between your models, which are set up using has_many and belongs_to calls in the relevant model files. Ignore the routes file for now as it’s not related to the main part of your question.

    With respect to associations, you’ll find that post.comments is not returning you an Array, it’s actually returning an ActiveRecord::Relation object. A Relation object is like an array – in fact any method you call on it which isn’t relation-specific (like build) is actually passed on to an Array representation of the Relation‘s contents. So, when you call post.comments.length, the comments association object is calling .length on its internal array.

    One of the consequences of this is that calling post.comments.class actually passes on the .class call to the Array too!

    So, what can that Relation object actually do? Since the association is set up by the has_many call, you can find out in the has_many documentation. Similarly for a belongs_to association

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

Sidebar

Related Questions

As title, I searched official google maps api reference and other site, I can't
Is there an official site where I can see all the java bugs for
I'm developing a site-specific Firefox extension. The official hosting/updating mechanism at addons.mozilla.org forces my
Ok, I have the HDF5 library downloaded from the official site, and I have
The official site shows the following function square = (x) -> x * x
The official site is quite scarce and most of the examples are EMF- related
I followed tutorial from cocos2d official site . I try to create some items
Looking at the quick start on the official site I wrote: less.html <!DOCTYPE html>
according to the official site, Android supports forward declarations from version 1.6 onwards. Having
I wanted to download solr 3.1 but the links from solr's official site are

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.