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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:21:09+00:00 2026-06-15T07:21:09+00:00

My comments model is pretty simple and works polymorphically, but I am now adding

  • 0

My comments model is pretty simple and works polymorphically, but I am now adding the ability to hide a comment by the author of a given record across those polymorphic associations.

class Comment < ActiveRecord::Base
  attr_accessible :content, :show
  belongs_to :commentable, :polymorphic => true
  belongs_to :user
end

So, requests, questions, posts, submissions etc… all have comments and are accessing the comments template without issue, but I want to allow the author of the content in those models to show or hide comments (as opposed to flagging, for example) when the application identifies them as the author of the content that is being commented on.

class Request < ActiveRecord::Base
   has_many :comments, :as => :commentable, :dependent => :destroy
end

So, I have everything working when there is only one model, by calling the author: @request.user,
but I’m wondering how to call an author using metaprogramming, so the comment view (with help) can determine what model is currently using the comment view.

I’ve done some research into metaprogramming, but have not found the answer.

Here is the code that calls the author (@request.user):

    <% if @comments %>
        <h1 class="mtop20">Comments</h1>
        <% for comment in @comments %>
           <% if signed_in? %>
                <% if comment.show == true %>
                   <div class="well comment mtop10">
                        <% if current_user == @request.user or current_user.has_role? :admin %>
                             <%= simple_form_for [@commentable, comment] do |f| %>
                              <div class ="">
                                <%= f.input :show, :as => :hidden, :input_html => { :value => false }  %>
                                <%= f.submit "Hide Comment", :class => 'btn btn-mini pull-right' %>
                              </div>
                            <% end %>
                        <% end %>
                        <span>
                           <%= image_tag comment.user.image.source(:header) %>
                           <%= link_to comment.user.name, comment.user %></span>
                           Posted <%= time_ago_in_words(comment.created_at) %> ago
                        </span>
                        <p class="mleft20 mtop10"><%= comment.content %></p>
                        <% if signed_in? %>
                            <% if current_user.id == comment.user_id or current_user.has_role? :admin %>
                                <%= link_to 'Edit', polymorphic_path([ comment.commentable, comment], :action => :edit), 
                                                    :class => 'btn btn-mini mtop5 mleft10' %>
                                <%= link_to 'Delete', [comment.commentable, comment],
                                                    :confirm => 'Are you sure?',
                                                    method: :delete, 
                                                    :class => 'btn btn-mini mtop5' %>
                            <% end %>
                        <% end %>
                   </div>
               <% end %>
               <% if comment.show == false %>
                  <p>A comment by <%= comment.user.name %> has been hidden by <%= @request.user.name %></p>
                  <% if current_user == @request.user or current_user.has_role? :admin %>
                     <%= simple_form_for [@commentable, comment] do |f| %>
                      <div class ="">
                        <%= f.input :show, :as => :hidden, :input_html => { :value => true }  %>
                        <%= f.submit "Show Comment", :class => 'btn btn-mini btn-success' %>
                      </div>
                    <% end %>
                  <% end %>
               <% end %>
          <% end %>
       <% end %>
    <% end %>
    <%= render "comments/form" %>
  • 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-06-15T07:21:10+00:00Added an answer on June 15, 2026 at 7:21 am

    use comment.commentable.user to access the author of your post.

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

Sidebar

Related Questions

I'm trying to add comments to a Post model class Comment < ActiveRecord::Base belongs_to
I've a simple model where class User < ActiveRecord::Base has_many :comments, dependent: :destroy has_many
Given the following view : def comments(request): comments_list = Thing.objects.filter(thing_type=2) #Thing model extends MPTTModel
I have a Comment model in my app but am encountering a lot of
The following model works fine for reads, updates, and deletes but not for creating.
so i've got one pretty simple method in a model here: def log self.statistics.build()
I'm split between writing my own comments model (pretty easy model, foreign key it
class Content(models.Model): author = models.ForeignKey('auth.User') stamp = models.CharField(max_length=50) class Comments(models.Model): content = models.ForeignKey(Content) message
My razor view page looks like: @foreach (var comment in Model.Comments) { @Html.RenderPartial(Comment, comment);
Assuming I have a comments model and a posts model, What code can I

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.