Ruby on rails can';t写入未知属性`user\u id`

Ruby on rails can';t写入未知属性`user\u id`,ruby-on-rails,database,model-view-controller,devise,migration,Ruby On Rails,Database,Model View Controller,Devise,Migration,我正在使用gemdesigne创建用户配置文件。每个用户都可以在我想在他写的评论下添加每个用户名的任何配置文件中创建评论 用户有很多评论 用户有许多配置文件注释,这是用户和注释之间的关系这是我在创建注释时遇到的错误 can't write unknown attribute `user_id` 这就是我所做的 class CreateProfileComments < ActiveRecord::Migration def change create_tabl

我正在使用gemdesigne创建用户配置文件。每个用户都可以在我想在他写的评论下添加每个用户名的任何配置文件中创建评论

用户有很多评论

用户有许多配置文件注释,这是用户和注释之间的关系这是我在创建注释时遇到的错误

can't write unknown attribute `user_id`
这就是我所做的

class CreateProfileComments < ActiveRecord::Migration
      def change
        create_table :profile_comments do |t|
    
          t.timestamps null: false
        end
      end
    end

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :text
      t.references :user, index: true, foreign_key: true
      
      t.timestamps null: false
    end
  end
end
在comment.rb中

class Comment < ActiveRecord::Base
  belongs_to :user #not users as you have defined in your question
  has_many :profile_comments
end
class ProfileComment < ActiveRecord::Base
  belongs_to :comment
  belongs_to :user
end
class注释
在profile comment.rb中

class Comment < ActiveRecord::Base
  belongs_to :user #not users as you have defined in your question
  has_many :profile_comments
end
class ProfileComment < ActiveRecord::Base
  belongs_to :comment
  belongs_to :user
end
class ProfileComment
在注释控制器中

class CommentsController < ApplicationController
  before_action :find_comment ,only:[:show,:update,:edit,:destroy]

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

  def index

  end

  def create
    @user =User.find(params[:id])
    @comment = current_user.comments.build(comment_params)
    @profile_comment = ProfileComment.new
    @user.profile_comments < @profile_comment
    @comment.profile_comment < @profile_comment
    if @comment.save
      redirect_to doctor_path(:id => @user.id)
    end
  end 

  def edit
  end

  def update

    if @comment.update(comment_params)
      redirect_to root_path, alert: "user Information updated successfully"
    else
      flash.now[:error] = "Couldn't update!"
      render :edit
    end
  end
  def destroy
    @comment.destroy
    redirect_to doctors_path, notice: "comment deleted Successfully"
  end 

private

  def find_comment
    @comment = Comment.find(params[:id])
  end

  def comment_params
    params.require(:comment).permit(:text)
  end
end
class CommentsController@user.id)
结束
结束
定义编辑
结束
def更新
if@comment.update(comment_参数)
重定向到根路径,警告:“用户信息已成功更新”
其他的
flash.now[:error]=“无法更新!”
渲染:编辑
结束
结束
def销毁
@毁灭
重定向到医生路径,注意:“评论删除成功”
结束
私有的
def find_评论
@comment=comment.find(参数[:id])
结束
def注释参数
参数require(:comment).permit(:text)
结束
结束
在用户配置文件视图中

      <% @user.profile_comments.each do | profile_comment |%>
  <%comment = profile_comment.comment%>
  <% if comment.text.present? %>
    <%= comment.text %><br>
    <%if comment.user.blank?%>
      No user assigned to this comment
    <%else%>
      <%= comment.user.name #or email or whatever%>
    <%end%>
    <br><hr>
  <% end %>
<% end %>


没有分配给此评论的用户


我猜您真正想要的只是创建两个指向同一个表的Assocation:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :text
      t.references :user, index: true, foreign_key: true
      t.references :author, index: true, foreign_key: { to_table: :users }
      t.timestamps null: false
    end
  end
end
两个
在您的用户模型中有许多关联:

class User < ApplicationRecord
  # comments about this user
  has_many :comments 
  # comments written by this user 
  has_many :authored_comments, 
    class_name: 'Comment',
    foreign_key: :author_id,
    inverse_of: :author
end
class用户
我不知道你为什么要加入另一张表,因为这两种关系都是一对多的关系

class CommentsController < ApplicationController
  before_action :set_user, only: [:new, :create]
  
  # ...

  def new
    @comment = @user.comments.new
  end

  def create
    @comment = @user.comments.new(comment_params) do |c|
      c.author = current_user
    end
    if @comment.save
      redirect_to doctor_path(id: @user.id)
    else
      render :new
    end
  end 

  private

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

  # ...
end
class CommentsController
如果要显示注释,请执行以下操作:

<% @user.comments.each do |comment| %>
<div class="comment">
  <div class="body">
    <%= comment.body %>
  </div>
  <p class="author"><%= comment.author.email %></p>
</div>  
<% end %>


ProfileComment
model/table,我不明白您最初是从哪里想到的。应该完成什么以及您认为需要它的原因?首先创建一个外部参照表,作为用户和为概要文件留下的注释之间的中介,并将其称为profile\u comments此profile\u comments表需要一个user\u id和一个integer类型的comment\u id来存储user和comments表中的主键,其中user_id是在个人资料中留下评论的用户的id。我有点困惑。请不要改名字。所以我这里有一些评论。为了更容易检查这个答案,因为我在代码中使用了它,请检查我附加的链接。答案一点也不好,这会让事情变得更容易。我想告诉你,你让事情变得比想象的更复杂。这是一个非常常见的场景,你需要两个混蛋恰好指向同一张桌子,你没有解释你想做什么,回答问题的人也不理解。哦,好吧,对不起。rails g迁移将列添加到注释作者:引用然后我在执行rake db:migrate PG::UndefinedTable时出现此错误:错误:关系“authors”不存在