[英]Rails self-join gives an error
I'm having troubles getting my friending model in Rails 4 working. 我在使Rails 4的朋友模型正常工作时遇到了麻烦。 I set up a friendship controller and modified my User model to use a self-join as follows.
我设置了一个友谊控制器,并修改了我的用户模型以使用自联接,如下所示。
Friendship Model: 友谊模式:
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: "User", foreign_key: "friend_id"
validates_presence_of :user_id, :friend_id
end
User Model: 用户模型:
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_many :items
has_many :friendships
end
My UserController: 我的UserController:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
load_and_authorize_resource
def index
authorize! :index, @user, message: 'Not authorized as an administrator.'
@users = User.all
end
def update
authorize! :update, @user, message: 'Not authorized as an administrator.'
if @user.update_attributes(params[:user])
redirect_to users_path, notice: "User updated."
else
redirect_to users_path, alert: "Unable to update user."
end
end
def destroy
authorize! :destroy, @user, message: 'Not authorized as an administrator.'
user = User.find(params[:id])
unless user == current_user
user.destroy
redirect_to users_path, notice: "User deleted."
else
redirect_to users_path, notice: "Can't delete yourself."
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit( :name, :email, :role_ids, :id, :user_id )
end
end
The relevant portion of my log: 我的日志的相关部分:
NoMethodError - undefined method friends' for nil:NilClass: app/views/users/index.html.erb:32:in
_app_views_users_index_html_erb__2019584293611114470_70229529361660' NoMethodError-未定义的方法
friends' for nil:NilClass: app/views/users/index.html.erb:32:in
_app_views_users_index_html_erb__2019584293611114470_70229529361660'
Unless I've missed this completely, I should be able to access a friend as @users.friends but that gives me a NoMethod Error. 除非我完全错过了这个,否则我应该能够以@ users.friends的身份访问朋友,但这会给我带来NoMethod错误。 I've also tried accessing it as @users.friendships but I get the same error.
我也尝试过以@ users.friendships的身份访问它,但遇到相同的错误。
I have run the migration and restarted my rails server. 我已经运行了迁移并重新启动了Rails服务器。 I don't know what I'm missing but I would sure appreciate a fresh set of eyes.
我不知道自己想念什么,但我一定会感激不尽。 I think it must be something with how I've set up the model.
我认为这一定与我如何建立模型有关。
The models look fine, however in the index
method on UsersController
you have the following line: @users = User.all
. 模型看起来不错,但是在
UsersController
的index
方法中,您具有以下行: @users = User.all
。 This creates an array of users which means you can't call .friends
on it. 这将创建一个用户数组,这意味着您无法在其上调用
.friends
。 You need to access each user individually through iteration 您需要通过迭代分别访问每个用户
@users.each do |user|
user.friends
# ... more code
end
or some other means. 或其他方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.