Topic: Advice to Rails Beginners: Follow Conventions
If I could offer only one piece of advice to you, it would be this: follow the Rails conventions
Learning Ruby on Rails can be difficult, but applying this advice will make it much easier. Rails is an awesome framework, but there's a lot more to it than what the framework itself has to offer. The conventions and practices Rails encourages is what I consider to be one of the best parts of Rails.
There are a lot of simple conventions in Rails that are some of the first things beginners learn such as making table names plural, class names singular, etc. This article will dive into a few conventions which may not be immediately obvious.
Choose a Name and Stick With It
One of the most important first decisions when building a Rails application is naming models. Once you name a model, the only variations of that name should be the plural and singular forms of the word. Stick with the full model name in everything - variable names, method names, controller names, etc.
If you find you want to rename the model halfway through the project, don't just start giving variables the new name. Go back and rename the model in every other part of the application to keep the consistency. This is a lot of work, but worth it. It really helps to get the name right the first time around.
One exception to this is the user interface. If you would like to present the model to the user under a different name, this is understandable. Just keep everything else consistent.
Tip: When naming an model, take a look at the reserved words to avoid problems in the future.
Keep Logic in Models
It seems many beginners have difficulty determining what code to place in models. As a result, the models are very small but the views and controllers are littered with business logic. More often than not, if you can move something out of the view/controller and into a model, you should! This has many benefits, including slimming down the views/controllers, removing duplication, and making it easier to test this logic.
For many applications there is a 1-to-1 mapping between controllers and models. The controller should have the plural form of the model. CRUD (create, read, update, delete) operations are added to the controller to perform actions on the model. If you unfamiliar with the CRUD operations, generate the scaffolding and take a look at the controller.
Try to keep a controller's operations limited to one model. If it starts messing around with other models, consider moving those operations into another controller.
Not all models have an associated controller, and not all controllers have every CRUD operation. It all depends on what functionality you need in the application.
Of course this 1-to-1 mapping between controller and model does not work for all situations. You are always free to create a non-restful controller for situations that do not fit into simple CRUD operations. For example, let's say there are Cart and Order models, and you would like to create a checkout process in your application. The checkout process is a multi-page form and does more than simply creating an order. Therefore, I recommend making a non-CRUD controller called "CheckoutController" to handle this checkout process.
Tip: If you are under edge rails (or 1.2), take a look at the "scaffold_resource" generator.
The Rails community has for the most part adopted the Ruby Language Conventions, and I recommend you do the same. Most likely these conventions are not the same as the ones you use in other languages, but I encourage you to give them a try. You will get used to them. By adopting the Ruby conventions, it makes it so much easier to read other code, and for someone to read yours. You may still be speaking the same language without these conventions, but it is like trying to understand someone with a strong accent.
We've only scratched the surface of Rails conventions in this article, but hopefully you've gained an understand of what they are. In all further articles, tutorials, and code samples, keep an eye out for good conventions and start applying them to your project.