last day (14 days later) » 

10:44 AM
0
A: NoMethodError on comment.user.name

Saurabh LodhaYou have to add the association between the models. Looks like you missed it if I am not wrong. You have the user_id column in the comments table but the association should also be set up in the models Comment Model class Comment < ActiveRecord::Base belongs_to :user end Also if you want to...

 
I have has many :comments with an s. Is that ok?
 
it should be has_many :comment. Just add the the two association in both the models and see if it works. It mostly will. :-)
 
They are here, but still the same error :-(
 
There is another chance that there is no user record for the comment you are trying. So the comment.user is returning nil. Hence when you try comment.user.name it throws error. Check if there is an user for the comment. If not then create a new user and associate the user to the comment by putting the value of user.id into comment.user_id
 
yea, when I replace the user.name with user_id, that shows up. so maybe there isn't a name associated with an id. How would I add one?
 
10:44 AM
comment.user_id = User.first and then comment.save. Also I think you are new to rails. I would recommend you go through Lynda's Rails Essential training. Its not free but certainly helpful.
Check this. comment.user.nil?. If the answer is true then you got your issue. You don't have any user associated to the comment.
 
hello there thanks for helping me out
I'm in a RoR bootcamp online and Im at the on-my-own-with-hints-stage
 
11:21 AM
have you solved it yet
 
12:03 PM
No not yet
 
whats the issue
?
 
It's still that undefined method on name in comment.user.name
I don't think I have a way to associate ids and names
but I'm new to rails so maybe its there, and I'm not accessing it right
 
what is the result of comment.user.nil?
 
It came up as true!
 
then there is no user
do this
u = User.new
u.name = 'Clyde'
u.save
comment.user_id = u.id
 
12:13 PM
in the terminal
 
comment.save
yes
in rails console
you know about rails console ?
 
yea cails c
rails c
 
great
then try the above commands in rails console
it would work
 
I was signed into a member user when I tried it
 
ok
is it a different model ?
 
12:17 PM
NoMethodError for comment
 
ok
comment = Comment.first
then the rest
this will assign the variable comment to the first record of Comment table
 
comment.user_id = u.id brought up a nil afterwards...
 
ok
type u and enter
see if u has any value ?
 
no user_id
 
ohh
then do comment and then enter
does it gives nil as result
 
12:22 PM
yes
 
then it means the comment variable has no value associated to it
comment=Comment.first
try this and again see the value of comment
 
yea, now its 4
 
ok
paste the lines that appeared
in the chat
let me see it
 
#<Comment id: 1, body: "Animi perspiciatis soluta dolore consequuntur cumq...", post_id: 29, created_at: "2014-12-13 11:54:35", updated_at: "2014-12-13 11:54:35", user_id: 4>
 
ok thats great
now check if there is a user that exists with id as 4
User.find(4)
so do
 
12:25 PM
yep
 
ok
now try comment.user
and paste the result in chat
 
#<User id: 4, name: "Cheyanne Willms", email: "kelly@okuneva.org", encrypted_password: "$2a$10$iu3OkggAtGHSEc3v6SPYPeuRmPuvkSGV3pSokE.OP8Q...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil,
 
great
 
I chopped off some irrelevent stuff
 
so you are good now
comment.user.name
this would give you Cheyanne Willms as value
 
12:28 PM
yea!
but what's wrong with the files
 
there is no user associated to the comment you are referring maybe
the simplest way to find out what's wrong
is to check the development.log file
there you would see the error
the open the console and try out the code
and then correct it there
in the file
 
oh man
this is a huge file
 
do one thing
where you are trying to print out the comment.user.name
just add
puts comment.id
above it
now when you try that file code again
you will have the id of the comment which is giving you the error
once you have that
 
then you could check in the console why it is not working
try to debug it that way
good luck
 
12:33 PM
ok thanks for your help!
 
no problem
also if your issue has been solved then mark the answer accepted
:-)
 
yes definitely!
 
 
1 hour later…
1:54 PM
you haven't accepted the answer yet
 
I'm not finished with it yet
 
 
2 hours later…
4:15 PM
cool
 

last day (14 days later) »