0

我有一个有很多朋友的用户模型。我正在尝试对朋友进行分页,但没有收到这样的列错误。解决方案似乎在这里得到了回答Rails, SQLException: no such column但我只是不太理解这个答案。

有人可以帮我解释什么是错的以及如何解决吗?

在我的 home.html.erb

<div class="span8">
    <ol class="friends">
            <%= render @friends %>
        </ol>
        <%= will_paginate @friends %>
</div>

在我的 _friends 部分:

<li>
    <span class="friendname"><%= friend.name %></span>
    <span class="friendid"><%= friend.friendid %></span>
</li>

控制器

def home
    @friends = current_user.friends.paginate(page: params[:page])
end

Current_user 在 session_helper 中定义为:

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 User < ActiveRecord::Base
   attr_accessible :name, :provider, :uid
   has_many :friends, dependent: :destroy
   validates :uid, presence:true, uniqueness:true
   .
   .
   .
end
4

2 回答 2

0

您应该将名为 user_id 的列添加到您的表中。为此,您必须创建迁移并运行它。

于 2013-02-25T20:44:26.340 回答
0

如果有人遇到这个问题,朋友模型正在寻找 user_id 作为关联两个模型的键。您可以通过键入在任何模型中设置主键,self.primary_key = 'name of column'然后使用 . 在第二个模型中设置要查找的列foreign_key=>'name of column from second model'

于 2013-03-03T17:59:43.437 回答