Rails Tutorial Chapter 11 error "uninitialized constant User :: Relationships"

I keep getting the error

   uninitialized constant User::Relationships

      

When working in chapter 11 of the rail guide.

Here is the complete error when trying to access the home page while logging into 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 went through the chapter several 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 done in the tutorial ... I added: follower_id back just in case and it still doesn't work.

And I also have a relationship_controller constructor.

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 occurs 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 piece of code before the second block works fine when I remove the second piece. For some reason, the “follower” relationship doesn't work. I played with it in the console and didn't call, whereas user.followed_users does work. I've been playing with this for four hours, dropped the table and rebuilt it and I can't get it to work.

I've tried looking at Stack Overflow before and found this:

Ruby error (uninitialized User :: Relationship constant)

But none of the solutions there helped. Thanks for any help!

+3


source to share


1 answer


You have a typo:

has_many :reverse_relationships, foreign_key: "followed_id",
                                 class_name: "Relationships",

      



Change the last bit to class_name: "Relationship"

.

+5


source







All Articles