Tutorial :What is the Rails way (convention wise)

Question:

Developing in rails is sometimes described as following "the rails way". And convention over configuration is also a very important aspect in rails.

But what are these specific conventions? Using active record for example?

Solution:1

I think the 'rails way' or 'ruby way' is the generally accepted syntax and practices that programmers used when writing in Ruby. All languages have similar sorts of 'best practice' habits (whether or not something is 'Pythonic' for example).

It's important to distinguish it from actual features or expected conventions used by the language or framework. It's kind of like the difference between the grammar rules of a written language and the stylistic guidelines that define a particular type of writing.

I think all languages have certain biases and strengths that make a certain 'style' of programming for it popular. People migrating languages might struggle at first because they haven't learned how to write to that particular language. For example, many programmers coming from a strictly OO environment might have trouble understanding how map(), inject() or lambda work in Ruby.

There's a lot of conventions used in Ruby/Rails that might seem alien to other platforms. The best way to get acquainted with the 'ruby way' is just to read a lot of source code and take notes on the way people seem to be constructing their classes, routines, file structure, etc.

Also reading the two 'Ways' books mentioned by others can't hurt. I own them both, and they're excellent resources.

Solution:2

The Rails Way is all about teaching "best practices" in Rails application design. It is run by Michael Koziarski, an independent consultant and member of the Rails core team and with extensive experience with both designing and building web applications.

Using detailed code examples, Obie systematically covers Railsâ key capabilities and subsystems. He presents advanced programming techniques, introduces open source libraries that facilitate easy Rails adoption, and offers important insights into testing and production deployment. Dive deep into the Rails codebase together, discovering why Rails behaves as it doesâ" and how to make it behave the way you want it to.

Solution:3

Solution:4

You don't have to follow the conventions, but you will be much happier if you do.

Lay out your directory structure the way Rails expects and it automatically knows where to look for things.

Name your tables as plurals of your models, and include an id column, and it will automatically know how to deal with your data.

Names your controllers, views, etc. as it expects and it will take care of so much of the drudgery that, given a choice, you'd be crazy not to.

In short, instead a big honking config file, just do things as Rails expects and the "configuration" is free. And it will make you a happier developer.

Solution:5

Worth remembering, because I tripped over it last week and spent quite some time unpleasantly confused, is that any column with a name that ends "_id" is treated as a foreign key and is not therefore included in content_columns. Quite reasonable, but I spent some time tracking down why my code wasn't exporting a value named "sg_deal_id". It's "sg_deal_num" now...

Solution:6

Rails was developed by DHH at 37signals. They've written a book on their best practices not only in coding, but also relating to things like design and business. You can read it free online. It is a good way to start seeing things from a I believe to be a common Rails developer perspective.

Solution:7

I think several posters focused too much on the 'rails way' literal words in the question.