Ten tips for Rails beginners

Sunday, October 09

So, you’ve finished the TODO lists, the cookbooks, and the ubiquitous byob (build your own blog) demonstrations from the Rails tutorial canon, and you’re impressed enough to begin building your first real-life Ruby on Rails application. It’ll be easy, you think. One hour—tops. Just like in the movie.

I bet at first, things are going smoothly. You’re generating controllers and models, you’re scaffolding, adding columns to the db, some validations here, some associations there, and life is good. You take a break, go have a cigarette, even, basking in the small victory of a rapid iteration. All that’s left to do now is get the user authentication system working… you know, that common thread that runs through the entire application. Easy, you think. You’ve read about that login generator that Tobias (xal) made. That’ll work.

Some hours later, after having gem install‘ed the login generator, gotten it working, realizing that you needed ACLs, poring over the wiki reading about implementing ACLs, and deciding to sit down with a cup of coffee and ActiveRecord’s api documentation, it hits you. You don’t know a lick of Ruby.

  • Tip the first: eventually, you will need to learn Ruby.
  • Tip the third: when you generate scaffolding, Rails will always pluralize the Controller name. Don’t let it confuse you.

$ ./script/generate scaffold Person

The above will make a people controller and a Person model. This doesn't happen if you name your model and controller explicitly.
  • Tip the fourth: you will make mistakes. And when you screw up the plural/singular conventions when using generators (and you will), you can destroy output generated by generators just as easily as you can generate it:

    $ ./script/destroy scaffold Person

  • Tip the fifth: if you only want two databases for your application (i.e. when developing), in your ./config/database.yml just reference your development connection under the production section:
development:
  adapter: mysql
  database: stickball_development
  host: localhost
  username: root
  password: secret

test:
  adapter: mysql
  database: stickball_development
  host: localhost
  username: root
  password: secret

production:
  development
  • Tip the sixth: do not use TAB to indent the word ‘development’ underneath the heading ‘production’ in database.yml. Despite its outward appearance, YAML is no ordinary text file. It’s special. If you find yourself getting an error like /usr/lib/ruby/1.8/yaml.rb:119:in `load': parse error, you might have TABs. Spaces are the answer.
  • Tip the eighth: you should subscribe to the mailing list. You will learn a lot in there. Oh, and search the archives before you post anything to see if your question’s been answered already. Or, you can just lurk, always reading - never posting - waiting for someone else to ask your question, just like you did in high school and at supper.
  • Tip the ninth: learn to look under the hood by running irb from inside your development environment. From your Rails application directory, run

$ ./script/console

and watch Rails load your environment up for you. Now you have access to all your Models and can do things like person = Person.find(:first) and anything else you might do in your Controllers.
  • Tip the tenth: watch your development.log file while running your application. Rails’ logging for the development environment will show you every sql query issued by ActiveRecord, every Action, and every View rendered, along with statistics.

$ tail -f ./log/development.log

As you watch the ActiveRecord queries scroll past, think quietly to yourself—‘heh, I didn’t have to write any of that sql’. Isn’t that nice?

Hopefully these tips will help you as you continue to learn the Rails framework. And if things seem overwhelming, take heart—frameworks can be a difficult thing to learn. You sort of have to figure out where things go, what features are available to you by virtue of said framework, and how to code within the framework’s paradigm. Eventually, though, you’ll know the framework by heart. Really, it won’t take long.

Comments

Leave a response

  1. PackagethiefOctober 20, 2005 @ 10:16 PM

    YAML aint a markup language.

  2. dirkNovember 22, 2005 @ 06:05 AM

    Thanks for Tip #6!!!!!!!!!!!

  3. kituneMarch 09, 2006 @ 01:00 PM

    Great !! Thanks for the tips :)