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 Generate a Scaffold

Curious about the automatic naming of files when generating a scaffold

In this video, we generate a scaffold using the following:

    bin/rails generate scaffold todo_list title:string description:text

This creates some files that are named based off of "todo_list" but they're generated as plural, "todo_lists".

Ex.

    app/controllers/todo_lists_controller.rb

I'm really curious as to why it automatically adds the 's' to the words I entered, and whether or not that ever causes problems under other circumstances. When it creates these files, does it follow certain naming conventions if the word I entered into the initial command already ends in an 's'?

If I had used the word "terms" instead of "todo_list", I assume it would know not to add an extra 's' to that. But what if I used "fish" or "potato" instead?

I realize the question itself isn't important in the grand scheme of things, but I can't help but wonder. I attempted a Google search and nothing really came up, so I'm hoping someone here might have some insight.

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

In general, Rails has some conventions, for example, the name of the model is always singular (User) and controller is always plural (users_controller), when treating the resource as singular, it accepts the singular form and when you mean plural, it does accept it in plural. There is a built-in 'pluralizer', so to speak, based on a dictionary, which is used for creating those plural forms. So it knows how to treat words that have irregular plurals, like children. I think it just adds 's' to words that are not in its dictionary, when for example you come up with a word like 'liker' or 'desktopper'. As long as you follow the conventions, you will have a great time with this :). I never tried creating scaffold or any such resource using plural in he command, so I'm not sure how it would react.

Thanks! I like the idea of it having some kind of built-in dictionary to work with and knowing how to pluralize most things. I think my mind just jumped to the worst-case scenario and I wondered what would happen it did something funky. It's good to know that this probably doesn't happen under most circumstances.

I appreciate the quick response. :)

Shreyash Agarwal
Shreyash Agarwal
9,843 Points

Brilliant! Was just looking up why the naming is such! Upvoted!