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
Mike Hickman
19,817 Pointshas_many and belongs_to confusion
Hi,
I'm working through challenge http://teamtreehouse.com/library/relationships-3
The test is:
Let's say we have a BlogPost model and a Comment model that represents comments someone might leave on a blog post. The comments table includes a blog_post_id column. Please setup the relationships correctly.
I looked for an answer on the forums and found it, but it makes no sense to me, so I'm looking for some clarification.
Somehow, the correct answer is:
class BlogPost < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :blog_post
end
I'm curious if you will magically always have RoR create random names like blog_post when you don't previously see it referenced anywhere. Looks like most people guessed to use belongs_to :blogpost with/without capitals and other variations.
Why is there an underscore out of nowhere, and is there a way to tell when these will be created/needed so we know when and where to place them?
Thanks for any info :)
Mike
1 Answer
Ethan Lowry
Courses Plus Student 7,323 PointsHi Mike,
The underscore isn't 'out of nowhere' - the BlogPost class name has a capital 'P' for 'Post', implying it's two separate words. Rails uses this convention, understandably, to decide that the class' 'snake-cased' name is :blog_post. If you had declared your class instead as Blogpost, you would have to use :blogpost.
Another example you'll come across is when plurals are used. For example, in this case, if you were saying a model 'has many blog posts', you would use:
has_many :blog_posts
Note the 's' on the end of 'blog_post' this time - this is another Rails convention - classes should generally be singular - Car, Animal, BlogPost, etc. - but Rails is smart enough to know when you reference something like has_many :cars, that you are declaring a one-to-many relationship between the current model and the Car model.
If you ever forget whether to use a plural or not, just read it like regular English: 'has one blog post' makes sense, but 'has one blog posts' doesn't, nor does 'has many blog post'.
Hope that helps clear things up a little.
Mike Hickman
19,817 PointsMike Hickman
19,817 PointsThanks for the explanation, Ethan. I appreciate you helping to clear that up.
Cheers,
Mike
Jeff Lange
8,788 PointsJeff Lange
8,788 PointsJust wanted to drop in to say thank you. I had found the answer to this same challenge, but never saw an explanation as to why we needed the underscore. Makes total sense now. Thanks again!
Ethan Lowry
Courses Plus Student 7,323 PointsEthan Lowry
Courses Plus Student 7,323 PointsMy pleasure Jeff, glad it helped.