I usually come across people asking what is the preferred way of laying out the properties of a model and, in fact, of an ActiveRecord model. Shall we put first the validations and then the associations? Or first the associations and then the validations? Or shall we put first the callbacks?
One can google for rails style guide
. Will basically find only this:
which is a good resource. But is not complete.
My Rails Model Style Guidelines
we are talking here about Rails 3
Here is the list of my styling guidelines and which I use whenever I am writing a Model.
This is the sequence of declarations:
attr_accessor
, andattr_reader
,attr_writer
for virtual attributes that are not derived from databasenilify_blanks
. I am using the nilify_blanks gem and here is where I put the corresponding directiveattr_accessible
, in order to declare the mass assignable attributes- associations
- all
belongs_to
, - all
has_one
, - all
has_many
(there is a gotcha here, read Note 1 below)
- all
- before validation callbacks
- validations
- after validation callbacks
- delegates
- scopes
- callbacks
- First the before,
- then the after,
- then the around
and according to the sequence that they take place. Read Rails Guides ActiveRecord Callbacks (there is a gotcha here, see Note 1 below) and EXCEPT the (before|after)_validation callbacks, since I put them earlier in the list
- public methods
- class methods
- instance methods
- protected methods
- class methods
- instance methods
- private methods
- class methods
- instance methods
Note 1: the [has_many
| has_one
| belongs_to
] associations allow you to define :dependent => :destroy
(though I do not use it on belongs_to….is not correct design … IMHO). This creates a before_destroy
callback.
The before_destroy
callbacks are executed in the sequence that they are defined. So, If you want a before_destroy
callback to take place before a before_destroy
callback of a has_one
| has_many
that destroys
the dependent objects, you will have to put the before_destroy
callback before the corresponding has_one
| has_many
.
This is a list of some extra guidelines:
Whenever I have
attr_accessor
(orattr_reader
, orattr_writer
orattr_accessible
) I put the list of corresponding attributes in alphabetical order. So, in long lists it becomes easier for me to locate a specific attribute.I tend to leave a blank line between two different groups of declarations. For example, when the validations block ends, I leave a blank line before I start the after validation callbacks.
I would be glad to hear about yours in order to improve mine too. (Comments are welcome on Google+)