Trevan Hetzel
Trevan Hetzel

Reputation: 1369

Rails - has_many :through not saving an attribute

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 %>

Upvotes: 0

Views: 960

Answers (1)

cbascom
cbascom

Reputation: 766

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

Upvotes: 2

Related Questions