0

Ok, am still a newbie in ruby on rails trying to learn my way around. I have two models (User model and Comment model). Basically a user has a simple profile with an 'about me' section and a photo's section on the same page. Users must be signed in to comment on other users profiles.

My User Model

    class User < ActiveRecord::Base
        attr_accessible :email, :name, :username, :gender, :password, :password_confirmation
        has_secure_password
        has_many :comments
        .
        .
    end

My Comment Model

    class Comment < ActiveRecord::Base
       belongs_to :user
       attr_accessible :content
       .
       .
     end

In my comments table, I have a user_id column that stores the id of the user whose profile has been commented on and a commenter_id column that stores the id of the user commenting on the profile.

Comment Form

    <%= form_for([@user, @user.comments.build]) do |f| %>
      <%= f.text_area :content, cols: "45", rows: "3", class: "btn-block comment-box" %>
      <%= f.submit "Comment", class: "btn" %>
    <% end %>

My comments Controller

    class CommentsController < ApplicationController
        def create
           @user = User.find(params[:user_id])
           @comment = @user.comments.build(params[:comment])
           @comment.commenter_id = current_user.id
           if @comment.save
             ......... 
           else
             .........
           end      
        end  
    end 

This works fine storing both user_id and commenter_id in the database. My problem comes when displaying the user comments on the show page. I want to get the name of the user who commented on a specific profile.

In my user controller

      def show
        @user = User.find(params[:id])
        @comments = @user.comments
      end

I want to get the name of the user from the commenter_id but it keeps throwing errors undefined method 'commenter' for #<Comment:0x007f32b8c37430> when I try something like comment.commenter.name. However, comment.user.name works fine but it doesn't return what I want. Am guessing am not getting the associations right.

I need help getting the correct associations in the models so as to get the name from the commenter_id.

My last question, how do I catch errors in the comments form? Its not the usual form_for(@user) where you do like @user.errors.any?.

routes.rb

      resources :users do
         resources :comments, only: [:create, :destroy]
      end
4

1 回答 1

0

在你的模型中尝试这样的事情

class User < ActiveRecord::Base
  has_many :received_comments, :class_name => "Comment", :foreign_key => "user_id"
  has_many :given_comments, :class_name => "Comment", :foreign_key => "commenter_id"
end

class Comment < ActiveRecord::Base
  belongs_to :user # comment about profile
  belongs_to :commenter, :class_name => "User", :foreign_key => "commenter_id"
end

退房: http: //guides.rubyonrails.org/association_basics.html

你可能会在 has_many 集合上想出更好的命名,收到和给出是我能在短时间内做的最好的:)

注意:在许多情况下,foreign_key 是选项,将其留在上面 - 我认为它有助于清晰

  • has_many fk 引用 many 表(其他表)中的列
  • belongs_to fk 指的是 many 表(这个表)中的列
于 2013-02-02T20:58:10.823 回答