title: How to access a deep nested objects in Ruby on Rails?
body: <p>Hello i am facing a problem with accessing objects that are nested too deep. I'm hoping one of you can help me out or faced the same issue? Because of my application navigation I can't re-factor my nesting objects in a more simple way because i always need to use the root object for example "Users"</p>
  
  <p>This is my route file:</p>
  
  <pre><code>resources :users do
      resources :posts do
        resources :comments
      end
  end
  </code></pre>
  
  <p>User model</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :posts
  end
  </code></pre>
  
  <p>Post Model</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
     belongs_to :user
    has_many :comments
  end
  </code></pre>
  
  <p>Comment Model</p>
  
  <pre><code>class Comment &lt; ActiveRecord::Base
    belongs_to :post
  end
  </code></pre>
  
  <p>Now when i try to access user's posts it goes all ok. It allows me to select User's posts all ok..</p>
  
  <pre><code>class PostsController &lt; ApplicationController
    before_filter :user
    respond_to :html, :xml
  
    def index
      @posts = user.posts.all
      respond_with(@posts)
    end
  
   protected
    def user
      @user ||= User.find(params[:user_id])
    end
  end
  </code></pre>
  
  <p>But when i try to access the User's Post Comment's i get an error...
  I want to select all comments that are related to the User's posts.. So here is what i did.
  My Comments Controller:</p>
  
  <pre><code>class CommentsController &lt; ApplicationController
     before_filter :user, :post
     respond_to :html, :xml
  
    def index
      @comments = post.comments.all
      respond_with(@comments)
    end
    protected
    def user
      @user ||= User.find(params[:user_id])
    end
  
    def post
      @post ||= user.posts.find(params[:post_id])
    end  
  end
  </code></pre>
  
  <p>The following error occurs when trying to access for example:
  <a href="http://localhost:3000/users/1/posts/3/comments/" rel="nofollow">http://localhost:3000/users/1/posts/3/comments/</a></p>
  
  <blockquote>
    <p>NameError (uninitialized constant
    Post::Comments):<br>
    app/controllers/comments_controller.rb:7:in
    `index'
    This is the route list that is relevant..</p>
  </blockquote>
  
  <pre><code>user_post_comments_index GET    /users/:user_id/posts/:post_id/comments(.:format)          {:action=&gt;"index", :controller=&gt;"comments"}
                             POST   /users/:user_id/posts/:post_id/comments(.:format)          {:action=&gt;"create", :controller=&gt;"comments"}
    new_user_post_comments GET    /users/:user_id/posts/:post_id/comments/new(.:format)      {:action=&gt;"new", :controller=&gt;"comments"}
   edit_user_post_comments GET    /users/:user_id/posts/:post_id/comments/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"comments"}
        user_post_comments GET    /users/:user_id/posts/:post_id/comments/:id(.:format)      {:action=&gt;"show", :controller=&gt;"comments"}
                             PUT    /users/:user_id/posts/:post_id/comments/:id(.:format)      {:action=&gt;"update", :controller=&gt;"comments"}
                             DELETE /users/:user_id/posts/:post_id/comments/:id(.:format)      {:action=&gt;"destroy", :controller=&gt;"comments"}
             user_posts GET    /users/:user_id/posts(.:format)                           {:action=&gt;"index", :controller=&gt;"posts"}
                             POST   /users/:user_id/posts(.:format)                           {:action=&gt;"create", :controller=&gt;"posts"}
           new_user_post GET    /users/:user_id/posts/new(.:format)                       {:action=&gt;"new", :controller=&gt;"posts"}
          edit_user_post GET    /users/:user_id/posts/:id/edit(.:format)                  {:action=&gt;"edit", :controller=&gt;"posts"}
               user_post GET    /users/:user_id/posts/:id(.:format)                       {:action=&gt;"show", :controller=&gt;"posts"}
  </code></pre>
  
  <p>Not sure if this can help but i've added a bit of the debug messages..</p>
  
  <pre><code>activerecord (3.0.3) lib/active_record/base.rb:1199:in `compute_type'
  activerecord (3.0.3) lib/active_record/reflection.rb:162:in `send'
  activerecord (3.0.3) lib/active_record/reflection.rb:162:in `klass'
  activerecord (3.0.3) lib/active_record/reflection.rb:198:in `quoted_table_name'
  activerecord (3.0.3) lib/active_record/associations/has_many_association.rb:102:in `construct_sql'
  activerecord (3.0.3) lib/active_record/associations/association_collection.rb:24:in `initialize'
  activerecord (3.0.3) lib/active_record/associations/has_many_association.rb:11:in `initialize'
  activerecord (3.0.3) lib/active_record/associations.rb:1492:in `new'
  activerecord (3.0.3) lib/active_record/associations.rb:1492:in `comments'
  actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.0.3) lib/abstract_controller/base.rb:151:in `process_action'
  actionpack (3.0.3) lib/action_controller/metal/rendering.rb:11:in `process_action'
  actionpack (3.0.3) lib/abstract_controller/callbacks.rb:18:in `process_action'
  activesupport (3.0.3) lib/active_support/callbacks.rb:450:in `_run__677074153__process_action__199225275__callbacks'
  activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `send'
  activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
  activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `send'
  activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
  actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  </code></pre>