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 ActiveRecord Basics Migrations and Relationships Relationships

Jack Riminton
Jack Riminton
651 Points

Error for the singular form of "belongs_to"

It is complaining that my relationship for the Comment model is not in the singular form, however I'm pretty sure that's what I have?

class BlogPost < ActiveRecord::Base has_many :comments end

class Comment < ActiveRecord::Base belongs_to :blogpost end

models.rb
class BlogPost < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :blogpost
end

1 Answer

Chris McKnight
Chris McKnight
11,045 Points

Your belongs_to does not use the correct snake case version of BlogPost. You need to use blog_post.

Alternatively, you can specify foreign_key, and class_name on your belongs_to. Then, you have to specify inverse_of on your has_many.

class BlogPost < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :blog_post
end