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


Undefined method for first_name after creating relationship in status.rb and adding first_name to show.html.erb

I get an undefined method error for "first_name" when attempting to add a new status or modify a status.

Looking at my migration files, my first table is "users" and first_name, last_name, and profile_name are added. When I'm attempting to create an input field for first name in show.html.erb I added the erb tag <%= @status.users.first_name %>

What could I be doing wrong?

Quick notes:

  1. My database is migrated
  2. The tags seem correct
  3. When I create a status, I get the undefined method error but if I refresh my statuses page, the status I attempted to create is there.
  4. When I try to edit a status and update, I get the same undefined method error again
  5. I'm using rails 4.0.0 and ruby 2.0.0p247 on Mac OSX Mavericks
  6. I've restarted my server
  7. Cleared my previous statuses via the rails console
  8. It might be useful to note that attr_accessible isn't in rails 4.0 so I don't have that field in any .rb files

Also, I apologize for the formatting. It looks like markdown code isn't working here.

5 Answers

Mike Morales
Mike Morales
19,833 Points

Sajad -

Wow! You've made it this far using ruby 2.0.0! Anyways, I ran into the same problem as you did. Delete all of your statuses if you haven't already done so. Type in rails console at the prompt, hit enter, type in u=User.first, enter. Your correct user is shown in the list of information. Use that User id to create a new statuses. See if that fixes your problem? It worked for me, though. Good luck!

Hey Mike, I didn't have any luck. It seems I'm experiencing a few issues. When I create user accounts, all of the parameters aside from email and password contain nil records. I managed to fix that using the following https://teamtreehouse.com/forum/problem-with-devise-in-simple-ruby-on-rails-app

Now, when I create user accounts, all of the information I input gets stored.

So I deleted all statuses, deleted all user accounts and started from scratch.

I can get to the statuses page, when I create a status and do not input an "ID" I get the same undefined method error for "first_name".

So I erased everything again, except for the user account which had an ID of 7 and went back to the statuses page to create a status. When I created the status and input the ID as 7, undefined method error for "first_name" again.


in statuses_controller.rb you have to permit user_id to be input by adding :user_id to line 72 like this

def status_params
      params.require(:status).permit(:name, :content, :user_id)
Nicolai Knoll
Nicolai Knoll
9,481 Points

sajad zada Thanks so much! This was the missing piece I was searching for the last two days!

Mike Morales
Mike Morales
19,833 Points

Awesome! Most of the issues, I've encountered were easily solved by correcting any small errors, I made on my part, or just by restarting the browser. But I know it's not always that simple to figure out.

I feel your pain. As I progress, I'm beginning to understand my errors more and more. I think half of the fight is just understanding the file structure and how everything works together.