turingschool / curriculum

Turing's main repository of tutorials and projects
385 stars 502 forks source link

at I2 I get undefined method when I type a.comments in command. #1247

Open darlingtonp4 opened 6 years ago

darlingtonp4 commented 6 years ago

I get 2.3.4 :001 > a = Article.first Article Load (0.2ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ? [["LIMIT", 1]] => nil 2.3.4 :002 > a.comments NoMethodError: undefined method `comments' for nil:NilClass from (irb):2 I don't know if this has to do with models generated being different from the ones shown in the site but I tried both ways and keep getting the same problem.

iandouglas commented 6 years ago
a = Article.first
# this returned 'nil', there was no article returned from the database
a.comments
# this throws an error because there's no "comments" method (or database association) 
# on the 'nil' that was returned and stored in your 'a' variable.