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 trialJohn Murphy
1,122 PointsMy 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:
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:
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
7,961 PointsIf you uncomment this line that should give you a more detailed stack trace that should so you the exact line that raises the error.
Raymond Wach
7,961 PointsThat 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
1,122 Pointshere is my github
https://github.com/woodyeire/clonyn
I cannot find anywhere in there where i have called the 'name' method :-/
John Murphy
1,122 PointsJohn Murphy
1,122 PointsOk, 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.
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 :-/