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:
- put
constants
in top. - then, put
filters
,validators
- then, comes
associations
- next, comes
scopes
- 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
Post a Comment