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 `first_name'. Rails error.

I'm on the "Creating Relationships" video of "Customizing Forms". I put the erb tag

    <%= status.user.first_name %>

into my statuses index view, but I get a undefined method 'first_name' error. The strange thing is, when I call

    <%= status.user.methods %>

first_name is listed as a method! What is going on?

The <%= @status.user.first_name %> <%= @status.user.last_name %>

in my show view works perfectly. I have

  belongs_to :users

in my status model. Database schema:

statuses table:

id | content | created_at | updated_at | user_id

users table:

id | first_name | last_name | profile_name | email | encrypted_password

2 Answers

Ah, fixed it. The model was wrong.

Should have been

belongs_to :user

not

belongs_to :users

What can cause annoyance is remembering when and where rails differentiates between singular and plural nouns when regarding database table names and entries. It's very clever, but I feel like i don't have enough control...ah well that's frameworks for you!

It's very clear when does Rails uses plural. As for example in the case of relations, you must use plural in one to many relationships, and you must use singular in many to one relations.

For example, in the case of your model a status can have multiple users, so you use plural. But if a status belonged to a single user, you should use singular :)