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
Tyler Kresch
Courses Plus Student 4,760 PointsCreating Relationships: User does not seem to have a user_id property
I have been following along with the videos for Build a Simple Ruby on Rails App, and I had just reached the Creating Relationships video of Customizing Forms.
I did the database migration to add a user to statuses and user_id as an index, but after putting the "belongs_to :user "line in status.rb, my erb code <%= @status.user.first_name %> on the status show page is generating an error:
undefined method `first_name' for nil:NilClass
I have embedded other attributes here, and it is even stable if I drop the ".first_name" from the end (though it doesn't show anything).
It seems to me that somehow the user is not being associated with the status, but this happens even after I clear out all old data and create new data.
I have created and deleted myself as a user once or twice, if that could be a confounding factor.
Any thoughts? Thanks.
3 Answers
Tyler Kresch
Courses Plus Student 4,760 PointsFigured it out. I didn't know which user number I was. After adding the collection, I was able to get it to work.
Thanks.
ecp
838 PointsHey @Tyler - that is super-fantastic! Glad you were able to figure that out :D
Brijesh Bharadwaj
4,705 PointsI still dont get it. I have the same problem. Could you please explain this in some detail?
ecp
838 PointsHi Brijesh,
I'm not sure how Tyler Kresch resolved the problem he was having. Just so you know, you can format your Forum post and replies using Markdown and you can tag students within forum posts. There's a Markdown Cheatsheet below where you're inputting your reply.
I've already tagged Tyler for you, but let's if one of the Teachers (Jason Seifer ) is available to answer your questions. Brijesh, mind explaining the problem you're running into? That would be super helpful in answering your question. Thanks.
Best,
Elizabeth
P.S. Don't forget, you can always email the Treehouse Support Team directly. The email address is help@teamtreehouse.com. :)
Tyler Kresch
Courses Plus Student 4,760 PointsHi Brijesh-
The reason for the error "undefined method `first_name' for nil:NilClass" is that I was trying to call a method (:first_name) on something that did not exist (nil). So when I said @status.user, I was asking the status which user it belonged to. Since There was no user, it returned 'nil'. By then calling @status.user.first_name, you are essentially doing nil.first_name and 'nil' does not have the :first_name method.
Double check that everything is happening properly up until you call @status.user. You want to make sure that:
1) The statuses table has a user_id column 2) That specific user_id corresponds to the primary key of a user that actually exists 3) Your models have the correct has_many and belongs_to relationships set up on them
My problem was actually #2 when I went through it. I had forgotten the :id of my user when I was testing it out.
Good luck!
Brijesh Bharadwaj
4,705 PointsTyler Kresch - But we never added user_id to the user model did we? or did devise add it on it's own?
Brijesh Bharadwaj
4,705 Pointsalso we never set up the user has_many statuses relationship either
Tyler Kresch
Courses Plus Student 4,760 PointsNeither actually. Every table has a special column called "id" which is also known as the "primary key" of the table. The first User you create will have an id of 1, the second one will have an id of 2, etc. The first Status you create will also ahve an id of 1 and the second one will have an id of 2, etc.
As far as getting it from the "user_id" column, Rails actually does some magic here to help you out. If I have set up the following relationships: Status belongs_to User User has_many statuses
and if @status is a particular status, then...
if you call @status.user, Rails will look at the statuses table and try to find a column called 'user_id'. Lets say the user_id for this particular status is 3. Rails then takes that 3 and goes and looks at the Users table and tries to find a user with the 'id' (primary key) of 3, and then returns that object.
If i had called @status.author, then Rails will look at the statuses table and try to find a column called 'author_id', which doesn't exist, so it will return nil instead.
So the user_id column on the statuses table is known as the 'foreign key' while it's on the statuses table, but it is the same number as the 'id' column, or the primary key, or the users table.
Does that help?
Brijesh Bharadwaj
4,705 PointsVery much! Thanks a ton :)
PS: Jim Hoskins missed out on creating the user has_many statuses relationship in the video.
Tyler Kresch
Courses Plus Student 4,760 PointsYou're welcome. :)
He might do it later, or it might not be important. In this case, we're only asking the status to find its user, so all we need is the Status belongs_to User line. If we wanted to ask the User what all of its statuses are (@user.statuses) then we would need the has_many part, since it adds the :statuses method to the User model.
Good luck!
Brijesh Bharadwaj
4,705 PointsBrijesh Bharadwaj
4,705 PointsHey Tyler, could you explain this in some detail. I'm stuck with the same error. Thanks.