Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

John Murphy
John Murphy
1,122 Points

My user_test.rb is failing on "Working with Associations"

I am working through the Treebook video series (which are brilliant, by the way) but I have hit a wall.

I am on the "Working with Associations" video and my user_test.rb is failing with the following error:

.......E
===============================================================================
Error: test_that_creating_friendships_on_a_user_works(UserTest)
: NoMethodError: undefined method `name' for nil:NilClass
test/unit/user_test.rb:62:in `block in <class:UserTest>'
===============================================================================


Finished in 0.13539522 seconds.

10 tests, 15 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

I have followed all of the instructions to a Tee and can't figure out why this is happening.

Below is my user_test.rb file:

user_test.rb
require 'test_helper'

class UserTest < ActiveSupport::TestCase
    should have_many(:user_friendships)
    should have_many(:friends)


    test "a user should enter a first name" do
      user = User.new
      assert !user.save
      assert !user.errors[:first_name].empty?
  end


    test "a user should enter a last name" do
      user = User.new
      assert !user.save
      assert !user.errors[:last_name].empty?
  end

    test "a user should enter a profile name" do
      user = User.new
      assert !user.save
      assert !user.errors[:profile_name].empty?
  end

    test "a user should have a unique profile name" do
      user = User.new
      user.profile_name = users(:john).profile_name

      assert !user.save
      assert !user.errors[:profile_name].empty?
  end


    test "a user should have a rofile name without spaces" do
      user = User.new(first_name: 'Sean', last_name: 'OMurchu', email: 'soulwoody@gmail.com')
      user.password = user.password_confirmation = 'password'
      user.profile_name = "My Profile With Spaces"

      assert !user.save
      assert !user.errors[:profile_name].empty?
      assert user.errors[:profile_name].include?("Must be formatted correctly")
  end

    test "a user can have a correctly formatted profile name" do
      user = User.new(first_name: 'Sean', last_name: 'OMurchu', email: 'soulwoody@gmail.com')
      user.password = user.password_confirmation = 'password'

      user.profile_name = 'seanomurchu1'
      assert user.valid?
    end

    test "That no error is raised when trying to access a friend list" do
      assert_nothing_raised do
        users(:john).friends
      end

    end

    test "that creating friendships on a user works" do
      users(:john).friends << users(:mike)
      users(:john).friends.reload
      assert users(:john).friends.include?(users(:mike))
    end



end

and here is my user.rb file:

user.rb
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :first_name, :last_name, :profile_name
  # attr_accessible :title, :body

  validates :first_name, presence: true

  validates :last_name, presence: true

  validates :profile_name, presence: true,
                           uniqueness: true,
                           format: {
                            with: /^[a-zA-Z0-9_-]+$/,
                            message: 'Must be formatted correctly'
                           }

  has_many :statuses, :dependent => :destroy
  has_many :user_friendships
  has_many :friends, through: :user_friendships


  def full_name
    first_name + " " + last_name
  end

  def gravatar_url
    stripped_email = email.strip
    downcased_email = stripped_email.downcase
    hash = Digest::MD5.hexdigest(downcased_email)

    "http://gravatar.com/avatar/#{hash}"
  end
end

I must highlight that I am using the c9.io platform for development. So far, everythings been fine with a little troubleshooting but this one has me stumped :(

Hope you can help!

2 Answers

Raymond Wach
Raymond Wach
7,961 Points

If you uncomment this line that should give you a more detailed stack trace that should so you the exact line that raises the error.

John Murphy
John Murphy
1,122 Points

Ok, this has helped me to find out where the error was coming from. Thanks! Looks as though it is a bug in the ActiveRecord version I was using with the version of Ruby I was running.

Error: test_that_creating_friendships_on_a_user_works(UserTest)
: NoMethodError: undefined method `name' for nil:NilClass
activerecord (3.2.18) lib/active_record/associations/has_many_association.rb:58:in `cached_counter_attribute_name'
activerecord (3.2.18) lib/active_record/associations/has_many_association.rb:54:in `has_cached_counter?'
activerecord (3.2.18) lib/active_record/associations/has_many_association.rb:62:in `update_counter'
activerecord (3.2.18) lib/active_record/associations/has_many_through_association.rb:53:in `insert_record'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:496:in `block (2 levels) in concat_records'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:344:in `add_to_target'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:495:in `block in concat_records'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:493:in `each'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:493:in `concat_records'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:134:in `block in concat'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:149:in `block in transaction'
activerecord (3.2.18) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.18) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:148:in `transaction'
activerecord (3.2.18) lib/active_record/associations/collection_association.rb:134:in `concat'
activerecord (3.2.18) lib/active_record/associations/has_many_through_association.rb:38:in `concat'
activerecord (3.2.18) lib/active_record/associations/collection_proxy.rb:118:in `<<'
test/unit/user_test.rb:61:in `block in <class:UserTest>'
activesupport (3.2.18) lib/active_support/testing/setup_and_teardown.rb:72:in `block in run'
activesupport (3.2.18) lib/active_support/callbacks.rb:425:in `_run__1132060749006781611__setup__767895579276996253__callbacks'
activesupport (3.2.18) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.18) lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
activesupport (3.2.18) lib/active_support/callbacks.rb:81:in `run_callbacks'
activesupport (3.2.18) lib/active_support/testing/setup_and_teardown.rb:70:in `run'

I referred to this article which describes a very similar issue.

http://stackoverflow.com/questions/23568084/create-with-has-many-through-association-gets-nomethoderror-undefined-method-n

However, I was unable to use their fix as I am not exactly experienced with RVM etc. and don't want to do anything too drastic that might corrupt my whole project :-/

Raymond Wach
Raymond Wach
7,961 Points

That error means that somewhere you have code calling the name method on a variable that has no value (the variable is nil). In the code you've posted, I don't see the name method called anywhere so I can't be much more helpful than that.

John Murphy
John Murphy
1,122 Points

here is my github

https://github.com/woodyeire/clonyn

I cannot find anywhere in there where i have called the 'name' method :-/