Rails Kitchen

It's a place to write on stuff I learned recently.

'Standard' for the Order of Associations, Scopes, Includes, Validations in a Rails Model

| Comments

Rails is all about ‘Convention over Configuration’. There are community-driven Ruby coding style guides. But there is no such convention of following a ‘standard’ for the order of arranging associations, scopes, includes, validations in a Rails Model. But we can create one for our project and be consistent with it in all the models. This is what I follow. Thanks to this stackoverflow post.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Model < ActiveRecord::Base   
  #all mixins
  include Something
  extend Something

  #constants 
  MAX_LIMIT = 10

  #other stuff
  acts_as_taggable
  paginates
  attr_accessor :something 
  before_create :some_method

  searchable do
    text :something
  end

  #associations
  has_many :something
  belongs_to :something_else

  #validations
  validate_presence_of :something

  #scopes
  scope :something

  #instance methods
  def instance_method
  end

  #class methods
  def self.method
  end

  #private methods
  private
  def method2
  end
end

Comments