Reputation: 3718
I have consistently been receiving the error
uninitialized constant User::Relationships
When working through chapter 11 of the rails tutorial.
Here is the full error when I try to access the home page while logged in in my browser.
Extracted source (around line #11):
8: </a>
9: <a href="<%= followers_user_path(@user) %>">
10: <strong id="followers" class="stat">
11: <%= @user.followers.count %>
12: </strong>
13: followers
14: </a>
I have gone through the chapter multiple times and checked every line of code, but sometimes your eyes play tricks on you so here is the rest of the code
users.rb
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationships",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
before_save { |user| user.email = email.downcase}
before_save :create_remember_token
validates :name, presence:true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX},
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6}
validates :password_confirmation, presence: true
def feed
Micropost.where("user_id =?", id)
end
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
This is the class itself
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
This is how it is in the tutorial... I added :follower_id back in just in case and it still didn't work.
And I also have a relationship_controller built.
class RelationshipsController < ApplicationController
before_filter :signed_in_user
def create
@user = User.find(params[:relationship][:follower_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
format.html { redirect_to @user }
fromat.js
end
end
end
And in routes...
resources :users do
member do
get :following, :followers
end
end
The page where the error is happening looks like this:
<% @user ||= current_user %>
<div class = "stats">
<a href ="<%= following_user_path(@user)%>">
<strong id="following" class="stat">
<%= @user.followed_users.count %>
</strong>
following
</a>
<a href="<%= followers_user_path(@user) %>">
<strong id="followers" class="stat">
<%= @user.followers.count %>
</strong>
followers
</a>
</div>
The first part of the code before the second block works perfectly when I removed the second part. It is just the "followers" relationship is not getting set up for some reason. I played around with it in the console and it wasn't calling, whereas user.followed_users does work. I have been playing with this for four hours, have dropped the table and rebuilt it and I can't get it to work.
I tried looking on stack overflow before and found this:
Ruby error (uninitialized constant User::Relationship)
But none of the solutions there helped. Thanks for any help!
Upvotes: 3
Views: 1399
Reputation: 16793
You've got a typo:
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationships",
Change the last bit to class_name: "Relationship"
.
Upvotes: 5