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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:23:16+00:00 2026-05-27T22:23:16+00:00

Disclaimer: first month of developing with rails, but I have read everything I could

  • 0

Disclaimer: first month of developing with rails, but I have read everything I could find.

Edit: Somehow I missed this very similar question with a similar final answer.

I have polymorphic flags:

Class Flag...
  belongs_to :flaggable, :polymorphic => true
  ...
end

I have nested resources that have the appropriate has_many :flags, :as => :flaggable statement.

resources :posts do
  resources :comments
end

I would like both posts and comments and in the future other things on the site to be flaggable. What is the DRY/standard way (I’m using Rails 3.1) to do this with regard to routes and controller?


What I did for routes:

Mostly based on this rails cast, I made flags as a nested resource of both posts and comments. Already, I think I’m on the wrong track because it seems to be re-stating the polymorphic relationships in the models as well as breaking the guideline that “Resources should never be nested more than 1 level deep.”

resources :posts do
  resources :flags
  resources :comments do
    resources :flags
  end
end

Alternatively, I thought to implement the flaggable routes separately as below. But again, this doesn’t seem DRY and additionally makes non-desired independent routes for comments.

resources :posts do
  resources :flags
end
resources :comments do
  resources :flags
end

Finally, I wondered if I could make a generic resource for flaggables. I couldn’t find any way to implement this and it has the same problem of the previous method of making general routes available for the generic flaggable type.

resources :flaggable do
  resources :flags
end

What I did for the controller for the nested resources above:

I implemented find_flaggable, but realized that with nested resources, the parameter that gets converted to a flaggable class could be either Post or Comment since both end up in parameters (post_id and comment_id). I could solve the below with an id priority list for the current setup, but that is not a general solution and makes it even less DRY than it already is.

def find_flaggable
  params.each do |name, value|
    if name =~ /(.+)_id$/
      return $1.classify.constantize.find(value)
    end
  end
  nil
end

So this is where I stopped (actually implemented a limited solution only for Posts and Comments) and realized I don’t know a satisfying way to accomplish this. Can anyone help?

  • 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-27T22:23:17+00:00Added an answer on May 27, 2026 at 10:23 pm

    Just to solve the bit of DRYing up the code in your routes, a simple way would be to use

    [:posts, :comments, :yet_another_resource].each do |resource_type|
      resources resource_type do
        resources :flags
      end
    end
    

    instead of

    resources :posts do
      resources :flags
    end
    resources :comments do
      resources :flags
    end
    

    This starts becoming really clean and useful when you have a lot of actions under your nested resources. Sort of like a simple version of the “Extract method” refactoring for your routes.

    EDIT

    In case you missed my comment, I think polymorphic_url is the right thing you are looking for!!

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

Sidebar

Related Questions

The Disclaimer First of all, I know this question (or close variations) have been
Disclaimer: I did try searching first, and even found this comment , but it
Disclaimer: I have no experience in signing the files and this is my first
First of all, a disclaimer: I have experience in other languages, but am still
Disclaimer: I have no prior experience in MVVM/MVC/MVP/MVWhatever, this is my very first try
First the disclaimer: I never learnt any programming in school, and just have to
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
Disclaimer:It's my first time developing an app that will be on a tablet style
Background / Disclaimer First of all, please feel free to skip this entirely if
First off, I need to give a disclaimer: DO NOT RUN THIS JAVASCRIPT It

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.