Ruby on rails 没有这样的列:user_id error rails好友分页

Ruby on rails 没有这样的列:user_id error rails好友分页,ruby-on-rails,Ruby On Rails,我有一个有很多朋友的用户模型。我试图给朋友们分页,但是我收到了一个没有这样的列错误。答案似乎在这里得到了回答,但我只是不太理解这个答案 有人能帮我解释一下什么地方出了问题以及如何解决吗 在my home.html.erb中 <div class="span8"> <ol class="friends"> <%= render @friends %> </ol> <%= will_

我有一个有很多朋友的用户模型。我试图给朋友们分页,但是我收到了一个没有这样的列错误。答案似乎在这里得到了回答,但我只是不太理解这个答案

有人能帮我解释一下什么地方出了问题以及如何解决吗

在my home.html.erb中

<div class="span8">
    <ol class="friends">
            <%= render @friends %>
        </ol>
        <%= will_paginate @friends %>
</div>
当前用户在会话\u助手中定义为:

def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
朋友模型

class Friend < ActiveRecord::Base
    attr_accessible :friendid, :name, :uid
    belongs_to :user
    validates :uid, presence: true
    validates :friendid, presence: true, uniqueness: true
end
class-Friend
用户模型

class User < ActiveRecord::Base
   attr_accessible :name, :provider, :uid
   has_many :friends, dependent: :destroy
   validates :uid, presence:true, uniqueness:true
   .
   .
   .
end
class用户
您应该将名为user\u id的列添加到表中。为此,必须创建迁移并运行它

如果有人遇到这个问题,friend model会寻找用户id作为关联这两个模型的关键。您可以通过键入
self.primary\u key='name of column'
在任何模型中设置主键,然后使用
foreign\u key=>'name of column from second model'

设置要在第二个模型中查找的列,为什么甚至需要一个列的用户id?“besides to:user”因此,您必须在friend中有用户id列。啊谢谢你的链接。基于此,我的用户模型中的
:foreign\u key=>“uid”,:association\u foreign\u key=>“uid”
会修复我的错误吗?我假设这将用户模型的uid(外键)连接到朋友模型的uid(关联外键)。是吗?你得试试。但是为什么不添加此列呢?这更简单。它不起作用……我不想添加那个专栏,因为rails做了很多幕后动作,我想了解发生了什么。有点让人困惑的是,我不能通过简单地选择哪一列表示将它们连接在一起的键来关联这两个表。我在两种型号上都试过:foreign_key=>“uid”,但都没用
class Friend < ActiveRecord::Base
    attr_accessible :friendid, :name, :uid
    belongs_to :user
    validates :uid, presence: true
    validates :friendid, presence: true, uniqueness: true
end
class User < ActiveRecord::Base
   attr_accessible :name, :provider, :uid
   has_many :friends, dependent: :destroy
   validates :uid, presence:true, uniqueness:true
   .
   .
   .
end