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.


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.

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.

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

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.

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

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.

Re: Advice to Rails Beginners: Follow Conventions

Do I have to know ruby before getting in rails?

Last edited by tahah (2009-12-30 10:00:09)

Re: Advice to Rails Beginners: Follow Conventions

tahah wrote:

Do I have to know ruby before getting in rails?

It would definitely help, but not required.. you can learn as you go.

Re: Advice to Rails Beginners: Follow Conventions

Dehlak wrote:
tahah wrote:

Do I have to know ruby before getting in rails?

It would definitely help, but not required.. you can learn as you go.

Thanks smile

Re: Advice to Rails Beginners: Follow Conventions

Hello friends I am bigginer in Ruby on Rails. I want to make an web application on it , I am using netbeans as an editor for codding. I have created one model, view and controller for one table of my database, but the problem is that when I am applying same proccess for the second table it creates model, and controller but I could not view this tables data and also could not do CRUD opration on it so please help me to get rid of from this problem.
I am waiting for usefull help and many-2 thanks in advance for thre guy's.

Re: Advice to Rails Beginners: Follow Conventions

Hey yall, newbie here. Can you install RoR on Xampp localhost using Windows 7?

Re: Advice to Rails Beginners: Follow Conventions

hello
i am new to ruby ,plz.,inform the best learning methods and recommended books,conventions,practices
tks

Re: Advice to Rails Beginners: Follow Conventions

rao157 wrote:

hello
i am new to ruby ,plz.,inform the best learning methods and recommended books,conventions,practices
tks

http://railsforzombies.org/
I started with this one. This gets you on the go, it is a great way to start it.

http://ruby.railstutorial.org/ Is also a good one to follow, although it is big. I've used a lot of it and I'm still busy with it. I am a newbie myself and I'm trying to get it going with this one.

http://speckyboy.com/2008/04/20/60-ruby … stsebooks/
You will see what is best for you.


Greets!

Re: Advice to Rails Beginners: Follow Conventions

thanks for the suggestion,
i am greatful if you could inform the best books to learn & practice the ruby before i jump to rails,since i don't have good oop experience
tks