Dave Olson
Dave Olson

Reputation: 252

Rails self-join gives an error

I'm having troubles getting my friending model in Rails 4 working. 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:

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'

Unless I've missed this completely, I should be able to access a friend as @users.friends but that gives me a NoMethod Error. I've also tried accessing it as @users.friendships but I get the same error.

I have run the migration and restarted my rails server. 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.

Upvotes: 0

Views: 114

Answers (1)

nhjk
nhjk

Reputation: 7830

The models look fine, however in the index method on UsersController you have the following line: @users = User.all. This creates an array of users which means you can't call .friends on it. You need to access each user individually through iteration

@users.each do |user|
  user.friends
  # ... more code
end

or some other means.

Upvotes: 1

Related Questions