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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:55:01+00:00 2026-06-15T19:55:01+00:00

Here are my models and associations: project class Project < ActiveRecord::Base … has_many :projectmemberizations

  • 0

Here are my models and associations:

project

class Project < ActiveRecord::Base
   ...
   has_many :projectmemberizations
   has_many :users, :through => :projectmemberizations
   has_many :project_comments
   ...
end

project_comment

class ProjectComment < ActiveRecord::Base
   attr_accessible :comment, :created_at, :project_id, :user_id, :user_name
   belongs_to :project
   has_many :projectcommentations
   has_many :users, :through => :projectcommentations

   def user
     self.user
   end
end

projectcommentation

class Projectcommentation < ActiveRecord::Base
   attr_accessible :comment_id, :project_id, :user_id, :user_name
   belongs_to :project_comment
   belongs_to :user
end

user

class User < ActiveRecord::Base
   belongs_to :account
   has_many :projectmemberizations
   has_many :projects, :through => :projectmemberizations
   has_many :projectcommentations
   has_many :project_comments, :through => :projectcommentations
end  

The question

So as you can see, users can comment on a project. That’s working fine. But what I’m trying to get working is displaying in the view WHO created a comment. Right now I’m trying to do this and it’s not producing any errors, but just not displaying anything for the user_name.

<% @project.project_comments.each do |comment|%>
   <div class="comment">
      <%= image_tag current_user.photo.url(:small) %>
      <strong><%= comment.user_name %></strong>
      <%= comment.comment %>
</div>
<% end %>  

Finally, I’ve got it working in the rails console if I do this:

comment = ProjectComment.first
comment.user_id = 1
comment.save
comment.user.name

So apparently it’s not saving the user_id when adding a comment? Sorry for the length of this by the way!

PS here’s she show and new actions in the project_comments controller:

def show
    @project_comment = ProjectComment.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @project_comment }
    end

  end

  def new
    @project_comment = ProjectComment.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @project_comment }
    end
  end

ProjectComment create method

 def create
    @project_comment = ProjectComment.new(params[:project_comment])

    respond_to do |format|
      if @project_comment.save
        format.html { redirect_to project_url(@project_comment.project_id), :notice => 'Project comment was successfully created.' }
        format.json { render :json => @project_comment, :status => :created, :location => @project_comment }
      else
        format.html { render :action => "new" }
        format.json { render :json => @project_comment.errors, :status => :unprocessable_entity }
      end
    end
  end

Code from view where comment is created

<%= form_for(@project_comment) do |f| %>
   <%= f.hidden_field :project_id, :value => @project.id %>
   <%= image_tag current_user.photo.url(:small) %>
   <%= f.text_area :comment, :class => "addcomment" %>
   <%= f.submit :value => "Comment", :class => "tagbtn" %>
<% end %>
  • 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-15T19:55:03+00:00Added an answer on June 15, 2026 at 7:55 pm

    Your template is currently displaying the contents of comment.user_name. Your rails console example that is working uses comment.user.name. You do have user_name in the attr_accessible list of attributes on the ProjectComment model, but none of the posted code fills in a value for that attribute.

    I believe from what you are describing that your ProjectCommentation model is unnecessary. The following models should give you what you are looking for:

    class Project < ActiveRecord::Base
        ...
        has_many :projectmemberizations
        has_many :users, :through => :projectmemberizations
        has_many :project_comments
        ...
    end
    
    class ProjectComment < ActiveRecord::Base
        ...
        attr_accessible :comment, :created_at, :project_id, :user_id, :user_name
        belongs_to :project
        belongs_to :user
        ...
    end
    
    class User < ActiveRecord::Base
        belongs_to :account
        has_many :projectmemberizations
        has_many :projects, :through => :projectmemberizations
        has_many :project_comments
    end
    

    In the ProjectComment controller:

    def create
        # If params[:project_comment] contains the user_id, it will be set here
        @project_comment = ProjectComment.new(params[:project_comment])
    
        # If your user id comes from somewhere else (like the user that is signed in)
        @project_comment.user_id = <user id here>
    
        # Normal save logic here
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are my models: class Project < ActiveRecord::Base has_many :gs_collectors, dependent: :destroy class GsCollector
Here are my models: class Deck < ActiveRecord::Base belongs_to :game has_many :deck_cards end class
Here are my models: **Resource** has_many :users, :through => :kits has_many :kits **User** has_many
I have my models & associations currently setup like so: class User < ActiveRecord::Base
I have set up 2 models as following : class Sector < ActiveRecord::Base attr_accessible
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here
Here is my models, class Age(models.Model): layer = models.CharField(max_length=50) order = models.PositiveIntegerField() ... class
Here's part of my Django app's models.py : class Person(models.Model): birth_year = WideYear(null=True, blank=True)
here is my model class gameUserTrophyInfo(models.Model): user = models.ForeignKey(userInfo) trophy = models.ForeignKey(gameTrophyInfo) date =
Here's a snippet from my application: class PortolioItem(models.Model): ... user = models.ForeignKey(User) contract =

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.