19

Edited this old question to include an answer from below: Rubocop has it https://github.com/rubocop-hq/rails-style-guide#macro-style-methods

Rails is all about 'Convention over Configuration'. However, I haven't come across a 'standard' for the order of associations, scopes, includes, validations, ... in Rails models yet. Take for example the following, simplified, product model:

class Product < ActiveRecord::Base
  mount_uploader :logo, AssetUploader
  acts_as_taggable
  paginates_per 50

  include ActionView::Helpers::NumberHelper

  belongs_to :company

  validates_presence_of [:title, :price, :plu]

  scope :on_website, where(display: true)

  def display_price
    ...
  end
end

Is this the correct order? It's maybe not that important to many people, but I personally think it would be great if there were a convention on this.

4
  • 4
    I'm not aware of such convention. Also, since ruby doesn't care, any order is correct. Jun 6, 2013 at 10:12
  • 1
    Don't you agree it would be nice if there were a convention?
    – lafeber
    Jun 6, 2013 at 11:45
  • I guess it doesn't matter as long as you keep related pieces together, so that it's easy to find them. Jun 6, 2013 at 11:47
  • 2
    Anyway, you can make up such convention and start promoting it. Who knows, maybe it'll settle. :) Jun 6, 2013 at 11:48

2 Answers 2

10

Rubocop has it https://github.com/rubocop-hq/rails-style-guide#macro-style-methods

class User < ActiveRecord::Base
  # keep the default scope first (if any)
  default_scope { where(active: true) }

  # constants come up next
  COLORS = %w(red green blue)

  # afterwards we put attr related macros
  attr_accessor :formatted_date_of_birth

  attr_accessible :login, :first_name, :last_name, :email, :password

  # Rails 4+ enums after attr macros
  enum gender: { female: 0, male: 1 }

  # followed by association macros
  belongs_to :country

  has_many :authentications, dependent: :destroy

  # and validation macros
  validates :email, presence: true
  validates :username, presence: true
  validates :username, uniqueness: { case_sensitive: false }
  validates :username, format: { with: /\A[A-Za-z][A-Za-z0-9._-]{2,19}\z/ }
  validates :password, format: { with: /\A\S{8,128}\z/, allow_nil: true }

  # next we have callbacks
  before_save :cook
  before_save :update_username_lower

  # other macros (like devise's) should be placed after the callbacks

  ...
end
1
  • 1
    Edited my old question to include your answer, and labeled yours as the correct one.
    – lafeber
    Sep 4, 2019 at 11:51
10

There is no such convention. But you can create one for your project and be consistent with it in all the models. This is what i follow.

class Model < ActiveRecord::Base
   #all mixins
   include Something
   extend Something

   #other stuff
   acts_as_taggable
   paginates

   #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
1
  • Thanks, @usha I was exactly looking for the same.
    – 2017kamb
    Apr 4, 2019 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.