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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:45:20+00:00 2026-05-13T10:45:20+00:00

I have a model, Report, that is polymorphic. So many itens in my site

  • 0

I have a model, Report, that is polymorphic.
So many itens in my site may have many of it.

And i would like to have a generic controller for posting it.
Its a very simple model, has only a text message and the association.

in my routes, im doing something like

map.resources :users, :has_many => [ :reports ]
map.resources :posts, :has_many => [ :reports ]

but in my reports_controller, i would like to get the relation from with its coming from.

like:

before_filter :get_reportable

def get_reportable
   reportable = *reportable_class*.find params[:reportable_id]
end

is this possible?

how could i get the reportable_class and the reportable_id?

I can get the params[:user_id] when it comes from users controller, or params[:post_id] when it comes from posts. I could do a case with all the relations, but it doesnt seem a clean solution at all…

having the polymorphic association would be the best, are there any how?

  • 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-13T10:45:20+00:00Added an answer on May 13, 2026 at 10:45 am

    If you have a single controller that processes requests through two differing paths, then you need to make it aware of the contexts in which it will be called. You often see a lot of code that looks something like this:

    before_filter :load_reportable
    
    def load_reportable
      if (params[:user_id])
        @user = User.find(params[:user_id])
        @reportable = @user
      elsif (params[:post_id])
        @post = Post.find(params[:post_id])
        @reportable = @post
      end
    rescue ActiveRecord::RecordNotFound
      render(:partial => 'not_found', :status => :not_found)
      return false
    end
    

    Since you’re using a polymorphic association, you may be able to do something like this instead:

    before_filter :load_reportable
    
    def load_reportable
      unless (@reportable = @report.reportable)
        # No parent record found
        render(:partial => 'not_found', :status => :not_found)
        return false
      end
    
      # Verify that the reportable relationship is expressed properly
      # in the path.
    
      if (params[:user_id])
        unless (@reportable.to_param == params[:user_id])
          render(:partial => 'user_not_found', :status => :not_found)
          return false
        end
      elsif (params[:post_id])
        unless (@reportable.to_param == params[:post_id])
          render(:partial => 'post_not_found', :status => :not_found)
          return false
        end
      end
    end
    

    The trouble with this approach, where you have one controller that serves two entirely different routes, is that generating error messages, such as “user not found” versus “post not found”. This can be tricky to get right if you’re not inheriting from a Users::BaseController, for instance.

    In many cases it’s easier to create two independent “reports” controllers, such as users/reports and posts/reports, where any common functionality is imported from a module. These controllers usually inherit from a base controller which performs the loading and error handling. The base controller can also establish layout, page title, etc., without having to re-implement this functionality for each sub-resources controller.

    The alternative is to de-couple reports and have it run as its own controller where the relationship to the “reportable” record is mostly irrelevant.

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

Sidebar

Related Questions

I have a view model like so that is created from my validator. public
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
Let's assume I have a model called product. Let's assume that product has three
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I have a model being populated by my data layer and then I have
I have some model objects I'm using in my Java client application. Later these
I have a model Question with a field called userid , before one ask
I have a model Foo which have a ForeignKey to the User model. Later,
Let's say I have this model named Product with a field named brand .

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.