Rails Forum
Rails Work - the best place to post and find great Ruby on Rails jobs.
Username
Password

You are not logged in.

New Posts in this thread
  • Index
  •  » Tutorials
  •  » Advice to Rails Beginners: Follow Conventions

#1 2006-10-31 04:11:40

ryanb
Moderator
Registered: 2006-06-14
Posts: 6323
Website

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.


CRUD Controllers

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.


Code :   - fold - unfold
  1. script/generate scaffold
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.


Code :   - fold - unfold
  1. script/generate scaffold_resource
Ruby Conventions

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.


Conclusion

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.


Railscasts - Free Ruby on Rails Screencasts

Offline

 

#2 2006-12-12 23:28:09

tortoise
Mechanic
From: West Virginia
Registered: 2006-11-01
Posts: 286
Website

Re: Advice to Rails Beginners: Follow Conventions

ryanb wrote:

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.

I'm in this situation. I want to rename one of my models, but I want to do it in the most painless way possible. Is it possible to just rename the table then rename the ruby class and that's that? Or do I have to drop the table and then add it again with the new name? Renaming methods, variables, files, etc etc I already got.

Offline

 

#3 2006-12-12 23:33:49

ryanb
Moderator
Registered: 2006-06-14
Posts: 6323
Website

Re: Advice to Rails Beginners: Follow Conventions

No need to drop the table, you can just call rename_table in the migration to do it. I don't know if it's necessary to rename the files and variables, but it's definitely recommended!


Railscasts - Free Ruby on Rails Screencasts

Offline

 

#4 2008-01-10 09:47:22

rola
Ticketholder
Registered: 2008-01-10
Posts: 2
Website

Re: Advice to Rails Beginners: Follow Conventions

ryanb wrote:

No need to drop the table, you can just call rename_table in the migration to do it. I don't know if it's necessary to rename the files and variables, but it's definitely recommended!

If this cannt solve the problem ,you need to drop the table.

Offline

 
  • Index
  •  » Tutorials
  •  » Advice to Rails Beginners: Follow Conventions

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson