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

Creating relationships between users and statuses fails using @status.user.first_name on show statuses view

After following the current tutorial on creating relationships I receive a No Method Error on the show.html.erb. It seems like the method in question is first_name. Could this be because of an issue with newer versions of devise?

undefined method `first_name' for nil:NilClass


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

18 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey all, sorry for the trouble here. It's best to following along with treebook using Rails 3.2.15+. There's a ton that's different between the two versions and the videos are written using Rails 3.2.X.

We just published a new course with Rails 4 specific content called Build a Todo List Application with Rails 4. Try checking that out as well.

Huh? "using Rails 3.2.15+" - what does that mean? Do you mean that you can use anything after 3.2.15 - is that the plus sign? You say use "3.2.15+" but then say the videos are written in Rails 3.2.x - what is ".x" - is that a version or do you mean anything after .X.? So confused and frutstrated when the videos fail and the forum is pretty useless. Can you just very clearly say how to install Rails so that this video series works (or ideally add a video) and where we can find the version of rails that works. With all the migrations, you can't just fix this. I've gone through this video all day twice now and now need to dump it and start over and I am still unclear how to start over so that I don't get stuck again on the first_name nil class error. Totally totally frustrated. Wish you would update this video or add a clear helper video for how to work around rather than letting people spend 20 hours working to a dead-end and then discover that they installed a non-working format only to have to start over.

Alright everyone, I've finally figured it out.

There are a few problems here. I will try to be as concise as possible.

If you go in to the rails console, run u=User.all and look at the user accounts that you had created. Do some of the fields, ie: first_name, last_name, contain nil values? If so go to A if not go to C.

A) 1. follow this guide to fix that https://teamtreehouse.com/forum/problem-with-devise-in-simple-ruby-on-rails-app 2. Once you've followed those instructions, go in to your rails console and destroy all user accounts using u=User.all.destroy_all

  1. In the rails console, delete all statuses using Status.delete_all
  2. Create a new user account
  3. Go back in to the rails console, run u=User.all and take note of your User id for the newly created account. That is the number you will input into the ID field when you're creating a new status
  4. Go back to your app, try to create a new status and include your ID. It should work. If not, and you get the same error, you have an issue with your statuses controller so continue on to B.

B) Open statuses_controller.rb. In line 72 you'll have to permit user_id by adding :user_id to permit

def status_params
      params.require(:status).permit(:name, :content, :user_id)
    end

Now, delete all of your statuses in the rails console again and retry. It should work fine at this point.

C) 1. In the rails console run u=User.all and take note of your User id.

  1. Run Status.delete_all to delete all of your statuses.
  2. Start your app and create a new status, input the ID number and content.

Thank you! This worked and it took me a while to do before.

There are a couple things to check here:

  1. Do you have the proper has_many and belongs_to methods in your models?
  2. You may need to delete all existing statuses from your DB. You can do this in rails console.

Most of the time, it is the latter issue causing the problem. Just make sure you delete all your statuses and then try again!

Normally the .rb files have an attr_accessible field with a few inputs already when you first open them. My status.rb and user.rb files didn't have an attr_accessible method in the files at all. I added them manually but then that lead to my rails server crashing.

This made me suspect that maybe the method is deprecated which turned out to be true after doing some sleuthing via google. This would lead to a few issues, since I added the methods manually, my rails server crashed, then I tried doing the relationship assignments for has_many and belongs_to to the attributes, server still crashing so I commented out the attr_attribute method and got the server running.

Now I'm stuck. My migration file does have t.string first_name, last_name, etc. defined. However, if I try to call <%= @status.user.first_name %> I get undefined method `first_name' for nil:NilClass which leads me to believe that either the issue is the deprecation or a migration issue.

I did delete all of the statuses through the console and then manually to no avail.

Cool. The other thing to check is in your statuses_controller in the new and create actions.

def create
  @status = @user.statuses.new(status_params)
  ...
end

This will create the association between the user and status in the database. Usually, there is a before_filter that gets the current user before this action so that @user will be available.

I think to help you anymore I will need to see more of your code. Is it on github?

I haven't fixed this issue yet guys. I took a break from rails and moved on to HTML5 mobile for now. I'll be jumping back on this after the thanksgiving break. In the meantime, if a staff member jumps in and addresses it, I would appreciate it.

Jim Hoskins and Jason Seifer maybe you guys might be able to step in and help us with this issue. It seems like a few other people are experiencing the same thing.

Here's my migration file

   class DeviseCreateUsers < ActiveRecord::Migration
    def change
      create_table(:users) do |t|
        t.string :first_name
        t.string :last_name
        t.string :profile_name

    ## Database authenticatable
    t.string :email,              :null => false, :default => ""
    t.string :encrypted_password, :null => false, :default => ""

    ## Recoverable
    t.string   :reset_password_token
    t.datetime :reset_password_sent_at

    ## Rememberable
    t.datetime :remember_created_at

    ## Trackable
    t.integer  :sign_in_count, :default => 0, :null => false
    t.datetime :current_sign_in_at
    t.datetime :last_sign_in_at
    t.string   :current_sign_in_ip
    t.string   :last_sign_in_ip

    ## Confirmable
    # t.string   :confirmation_token
    # t.datetime :confirmed_at
    # t.datetime :confirmation_sent_at
    # t.string   :unconfirmed_email # Only if using reconfirmable

    ## Lockable
    # t.integer  :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
    # t.string   :unlock_token # Only if unlock strategy is :email or :both
    # t.datetime :locked_at


    t.timestamps
  end

  add_index :users, :email,                :unique => true
  add_index :users, :reset_password_token, :unique => true
end
end

There should be a user_id column on your statuses table.

Here's the other migration file which added the user_id column

class AddUserIdToStatuses < ActiveRecord::Migration
  def change
    add_column :statuses, :user_id, :integer
    add_index :statuses, :user_id
    remove_column :statuses, :name
  end
end

Added ya to the repo!

Hi S Zada, were you ever able to resolve this issue? I'm having the same problem. I've tried everything suggested to you in this forum but haven't been able to fix it.

Josh Flowers
Josh Flowers
7,010 Points

I'm stuck with this problem as well. Treehouse needs to redo this course using Ruby 2 and Rails 4

Yes, it would be nice if we could get one of the Staff to help address how to do this with Rails 4

Liju George
Liju George
5,433 Points

Yup Hope they are working on it..i'm stuck as well :(

Alex Romanillos
Alex Romanillos
16,144 Points

For me the problem occurred when calling the <%= status.user.full_name %>

If you are in the same situation, most likely you already fixed the parameter access in the last chapter.

So the solution was that I was following the video, and used the user id= "1". But what I didn't realised is that my users table in the database had my user row with ID = 5. Therefore the Nill error.

So go to the rails console, do u = User.all and see which ID you have in your database. This might fix the problem for you as well.

it's 2016 and the problem has not been resolved, nice!