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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:28:56+00:00 2026-05-30T16:28:56+00:00

I am using polymorphic association. I have 2 models articles and events which have

  • 0

I am using polymorphic association.

I have 2 models articles and events which have has_many association with comments model using polymorphic association

Inside comments controller :

def index

  @commentable = find_commentable

  @comments = @commentable.comments

end

def find_commentable

  params.each do |name, value|

    if name =~ /(.+)_id$/

      return $1.classify.constantize.find(value)

    end

  end

  nil

end

Inside article and event model i wrote:

has_many :comments, :as => :commentable,:dependent => :destroy

Inside comments model:

belongs_to :commentable, :polymorphic => true

Inside routes :

resources :articles do
resources :comments

end

my problem is

1) When I enter http://localhost:3003/articles/8/comments it gives the following error:

undefined method `comments' for nil:NilClass

2) Also I dont want to use nested routes, I want to use simple routes. How can I achieve the same thing as this code is doing?

  • 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-30T16:28:57+00:00Added an answer on May 30, 2026 at 4:28 pm
    1. You’re receiving this error because @commentable is nil. Do you have an article with an ID of 8? Is find_commentable function is returning the commentable correctly? My guess is the answer to one of those two questions is “no.”

    2. If you don’t want nested routes, you can just do resources :comments but you should be sure you pass in commentable_type and commentable_id as params to the controller; otherwise you won’t be able to look up the commentable class to get its comments.

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

Sidebar

Related Questions

I am using Polymorphic Models . Simple Question: My code below works without using
When I render XML with an :include clause for a polymorphic association I have,
Right now, I have three models Post, Comment and User (using Devise ) associated
Using Ruby on Rails, how can I achieve a polymorphic has_many relationship where the
In my project I currently have different models (Project, Message, etc) that: has_many :assets,
Using S#arp Architecture 1.0RC... I have a controller that never seems to update one
I have two models, Article and Post that both inherit from a base model
I am using polymorphic associations to track Comments in my project. All very straight
This might be a tough one I have a site which is using a
If I have a polymorphic association, how do I access related methods in my

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.