Best practice to organize model code in ruby on rails -


in rails model can write many things in single file. (sample model)

sample model:

class payment < activerecord::base    # database assosication   has_many :post   belongs_to :user    # filter   before_save :check_full_name    # validation   validates_presence_of :name    # scope   scope :is_paid, -> { where(:status => 'paid') }    # constents   status = {       :paid => 'paid',       :pending => 'pending',       :failed => 'failed'   }    # methods   def get_name     # sample code goes here   end  end 

in model use database association, filter, validation, scope, functions.

but best way organize model.

i mean should go first association ? or validation or scope?

i don't know if there best practice guideline this. share experience.

what is:

  1. put constants in top.
  2. then, put filters , validators
  3. then, comes associations
  4. next, comes scopes
  5. and after that, methods

also, while putting them, keep them in alphabetical order of names.

we follow structure in our codebase , works us. but, it's personal/team choice of organizing things in model.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -