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

undefined method `full_name' for nil:NilClass --- <%= @status.user.full_name %>

I keep getting this error despite deleting all users in the db and re-running the server. Additionally, I can't seem to figure out why the error keeps getting thrown...

I have done everything that's been posted on the forum: My user id is 5 and this error gets thrown when I put in the correct user id. I need some help on this as I've exhausted every possibility...from reading the file in sqlite manager and using try(:full_name)

My code for the following pages:

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

  attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name,
                  :last_name, :profile_name

  def full_name
    first_name + " " + last_name
  end

end  ``` 


  ```status.rb

class Status < ActiveRecord::Base
    attr_accessible :content, :user_id
    belongs_to :user
end ``` 


  ```show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @status.user.full_name %>
</p>

<p>
  <strong>Content:</strong>
  <%= @status.content %>
</p>

<%= link_to 'Edit', edit_status_path(@status) %> |
<%= link_to 'Back', statuses_path %> ``` 


  ```Indext.html.erb

<div class="page-header">
<h1>Statuses</h1>
</div>

<%=link_to "Post a New Status", new_status_path, class: "btn btn-success" %>
<% @statuses.each do |status| %>
<div class="status">
  <strong><%= status.user.full_name %></strong>
  <p><%= status.content %></p>
<div class="meta">
  <%= link_to time_ago_in_words(status.created_at) + " ago", status %>
  <span class="admin">
  |  <%= link_to "Edit", edit_status_path(status) %> |
    <%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure you want to delete this status?"} %> 
</div>
</div> ``` 


  ```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

  attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name,
                  :last_name, :profile_name

  def full_name
    first_name + " " + last_name
  end

end ```

6 Answers

Shaun -

Did you try typing, rails console at the prompt and then the next prompt type in, u=User.first? Typing in u=User.first will show you your correct User id: ?. Try deleting all of your statuses again and then type in your correct User id #, and see if that works.

Hello Mike, I did type the rails console and get u=User.first to remove the users. Also, the user id shown on the console is 5. I also tried deleting all statuses and typing in the correct User id#.

``` u = User.first User Load (1.9ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 => #<User id: 5, first_name: nil, last_name: nil, profile_name: nil, email: "shaunktw@gmail.com", encrypted_password: "$2a$10$wvH7uDaKI1FvS7YpzOOZsuFnAHFJKJLmxssKY.DNUpVL...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2013-12-24 15:01:49", last_sign_in_at: "2013-12-24 15:01:49", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-12-24 15:01:49", updated_at: "2013-12-24 15:01:49">

This is what I got from User.first.

Also, my statuses are the following:

  ```Status.all
  Status Load (0.3ms)  SELECT "statuses".* FROM "statuses"
 => #<ActiveRecord::Relation []>  ``` 

And when I enter 5 as the User ID in the edit page and try posting it..I still get that NilMethod Error:

   ```  undefined method `full_name' for nil:NilClass
Extracted source (around line #5):
2
3
4
5
6
7
8


  <p>
    <strong>Name:</strong>
    <%= @status.user.full_name %>
  </p>

  <p>  ```

Have you tried deleting your @status.user.full_name and leaving that part blank in your show.html.erb file, and also delete your status.user.full_name and rename it, "name" in your index.html.erb file between your strong tags? And then delete all of your statuses, and start over from there to see if that helps, or not?

Yeah I have...it's okay, I am going to re-do the project again. When I looked at the tables in sqllite manager, somehow the user ID isn't getting parsed into the status table..there's not association.

I had to restart from scratch numerous of times, I think it's better to learn that way, though. Just make sure you're using a previous version of ruby on rails or otherwise if your using the latest version you will run into problems.

Good luck, man!

Yeah I am running Ruby 2.0.0...I had issues downloading the same version treehouse is using. Maybe that could be the reason.

I had issues running Ruby 2.0.0. Are you using Windows or are you on a Mac or a Linux flavor?

This is the link: Ruby on Rails I used to set up ruby on rails, and so far it is working good. But I am using Ubuntu and a virtualbox since I don't have a Mac.

I am using a Mac to set up everything up

Even better, if you own a Mac!

I finally got everything to work...re-did the project and found this thread on the forum https://teamtreehouse.com/forum/creating-relationships-between-users-and-statuses-fails-using-statususerfirstname-on-show-statuses-view-- apparently I missed this one out.