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.
Offline
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
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!
Offline
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