29577

Ruby error (uninitialized constant User::Relationship)

I am new to ruby and trying to work through Michael Hartl's http://ruby.railstutorial.org/. I am on chapter 12 and keeping coming upon this error

uninitialized constant User::Relationship

What does this type of error mean? What do you think is likely my mistake?

attr_accessor :password attr_accessible :name, :email, :password, :password_confirmation, :admin has_many :microposts, :dependent => :destroy has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy has_many :following, :through => :relationships, :source => :followed has_many :reverse_relationships, :foreign_key => "followed_id", :class_name => "Relationship", :dependent => :destroy has_many :followers, :through => :reverse_relationships, :source => :follower email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :name, :presence => true, :length => { :maximum => 50 } validates :email, :presence => true, :format => { :with => email_regex }, :uniqueness => { :case_sensitive => false } validates :password, :presence => true, :confirmation => true, :length => { :within => 6..40 } before_save :encrypt_password def has_password?(submitted_password) encrypted_password == encrypt(submitted_password) end def feed # This is preliminary. See Chapter 12 for the full implementation. Micropost.where("user_id = ?", id) end def following?(followed) relationships.find_by_followed_id(followed) end def follow!(followed) relationships.create!(:followed_id => followed.id) end def unfollow!(followed) relationships.find_by_followed_id(followed).destroy end class << self def authenticate(email, submitted_password) user=self.find_by_email(email) (user && user.has_password?(submitted_password)) ? user : nil end def authenticate_with_salt(id, cookie_salt) user = find_by_id(id) (user && user.salt == cookie_salt) ? user : nil end end private def encrypt_password self.salt = make_salt if self.new_record? self.encrypted_password = encrypt(self.password) end def encrypt(string) string end def encrypt(string) secure_hash("#{salt}--#{string}") end def make_salt secure_hash("#{Time.now.utc}--#{password}") end def secure_hash(string) Digest::SHA2.hexdigest(string) end end

Answer1:

It sounds like maybe you didn't update the User model as specified in Listing 12.11.

Specifically, make sure your User class has this code:

has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy

Answer2:

You don't have the relationship_controller.rb code defined yet. Keep working through the chapter, he does explain what needs to be done eventually.

Answer3:

Ran into this myself. I hadn't added the Relationships controller (app/controllers/relationships_controller.rb) which you can find at 11.34. Once that's added the error will disappear.

Recommend

  • Implementing a “Follow” user feature using PHP and Mysql
  • Rails 4 Validating email uniqueness without case_sensitive
  • Rails 3.1: Custom validation message on join table of has_many through relationship?
  • marshmallow: convert dict to tuple
  • Validation between two date rails
  • Why don't I understand the Rails validates presence from tutorial?
  • Add validation rule to a class without using the form validation?
  • Querying for followers in news-feed-based data model in Django
  • jquery validation based on drag & dropped items
  • RAILS 3 model validation inclusion in does not work
  • Can I know how much followers I had on my twitter account in a specific date?
  • validates_attachment for optional field
  • passport-azure-ad, does it parse & validate token?
  • How to create constructor with optional parameters? [duplicate]
  • Carrierwave not uploading - no error shown
  • Follow yourself in a stream timeline (getstream.io)
  • How to add a User model validation to block certain email domains
  • No route matches [PATCH] “/contact.1”
  • Twitter like Model using SQL Server/Azure or Graph DB
  • What's the difference between uniqueness case_insensitive:true and case_sensitve:false?
  • Chain of SQL subqueries within large query of JOINS
  • Ruby error (uninitialized constant User::Relationship)
  • Twitter API: Who unfollowed from me?
  • Active record in standalone Ruby
  • Mongoid same embedded documents types for different fields
  • Handling null values with wso2 CEP
  • Many to Many self Join with Entity Framework Code First
  • Get the followers on a post efficient way in laravel 5.1
  • Associate git repo with existing fork network
  • How to determine the association between a VB6 app and an exe instanced with CreateObject()
  • How to specify a multi-column UNIQUE constraint in code-first Entity Framework fluent API
  • Detaching entity along with referenced entities
  • How to make Javascript generated checkboxes persist?
  • ASP.NET RegularExpressionValidator, validate on a non-match?
  • CoreData basics – to-many relationship array data
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • Email format validation in mvc3 view
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?