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 Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Relationships

Kenan Xin
Kenan Xin
16,139 Points

why "has_many :todo_items" when there is only todo_item.db? Why not has_many:todo_item?

why "has_many :todo_items" when there is only todo_item.db? Why not has_many:todo_item?

I remembered generating the model "rails generate model todo_item todo_list:references content:string" I cant find todo_items anywhere, where does it come from?

class TodoList < ActiveRecord::Base
    has_many :todo_items
    validates :title, presence: true
    validates :description,presence: true
    validates :title, length: { minimum: 3 }
    validates :description,length: { minimum: 5 }
end

I would really appreciate if someone could explain to me what is going on T.T

1 Answer

Raymond Sapida
Raymond Sapida
33,049 Points

Hi there,

It's part of rails conventions to pluralize names when they're used as controller names and when they're used in has_many associations. For example, a scaffold for your generated model would have a controller named todo_items_controller.rb and would be referenced as has_many :todo_items. It doesn't work the other way where it would be belongs_to :todo_item

If you're interested in how rails accomplishes this, the pluralize method is located in ActiveSupport::Inflector.

I hope this was helpful and good luck!