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

Lesson trouble. - Creating relashionships

Currently on this lesson and getting the following error - NoMethodError in Statuses#show Showing /home/action/treebook/app/views/statuses/show.html.erb where line #5 raised:

undefined method `first_name' for nil:NilClass Extracted source (around line #5):
<p> <strong>Name:</strong> <%= @status.user.first_name %>

</p> Rails.root: /home/action/treebook I have deleted all statuses from rails console and all users and created new user and still getting this error. Any advice would be great as started the project again 3 times to ensure its not my error. Also a side note its hard to follow along as some things that you put in your files no longer valid now like the attr_accessible :content, :user_id when i enter this i get error message if i remove it i dont. Please advise.Thanks in advance Jim Hoskins Jason Seifer

4 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Adam Flanagan here's how to change your rails version:

gem uninstall rails
gem install rails --version=3.2.16

If you're using rbenv do rbenv rehash right after that. From there you should be able to follow along starting with the rails command.

Brandon Barrette
Brandon Barrette
20,485 Points

So this means, either @status is nil, so therefore @status.user is nil, so you can't call full name on it. OR, there is no user_id on the status, so therefore calling @status.user returns nil (because there is no user).

I suggest going to the terminal. Go into rails console (type rails console).

Then try this and see what happens:

S = Status.last
S.user
S.user_id
S.user.full_name

What is the output for those 4 lines?

Thanks so much for a response. I have started the project again and i'm almost at the same point now. If it fails again i will do those commands and post results.

Again thanks

I do have a question if you don't mind while i'm working my way back through again. lesson this lesson at 03:04 shows the users.rb file. It the version on the video it has the line starting attr_accessible blah blah, where as mine does not, why is this? Also when i add it nothing works so i leave it out? Any advice?

Brandon Barrette
Brandon Barrette
20,485 Points

Are you using Rails 3.2? Attr_accessible tells rails which columns of the database you are allowed to write to. In rails 4 this moves out of the model and gets put somewhere else. If you are not using rails 3.2 I recommend using it because some things have changed and will not match the videos.

Yeah i deleted by progress and was going to start again with Rails 3.2 today to see if it works then, which i assume it will.

Thanks

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Adam Sackfield, Brandon Barrette is right -- the lessons are using 3.2 and not 4.0. The attr_accessible lines don't work in version 4.0. I recommend following along using Rails 3.2 since that is what the lessons use and will be the easiest way to keep track of everything in the videos.

Edit: Also, check out this video where Jim fixes a similar error. It may help.

As an aside, we're almost done with some Rails 4.0 content that should be out very soon :)

Josh Flowers
Josh Flowers
7,010 Points

I can't wait! I'm so stuck and just want to start over at this point.

Jason Seifer does that mean I have to start from scratch? I've already done that 5 times and am getting really impatient! Please advise!!!

If you are not using Ruby 1.9 and Rails 3.2 then you will have to start again Adam Flanagan

Okay, I'm about to start again, how do I change my rails version? Do I download the old version or what??

Jason Seifer does that mean I have to start from scratch? I've already done that 5 times and am getting really impatient! Please advise!!!