Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joshuapinter
Last active November 20, 2023 12:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save joshuapinter/4241250 to your computer and use it in GitHub Desktop.
Save joshuapinter/4241250 to your computer and use it in GitHub Desktop.
Rails Model Template
class [Your Class Here] < ActiveRecord::Base
##################################################################################################
### Attributes ###################################################################################
##################################################################################################
### Constants ####################################################################################
##################################################################################################
### Includes and Extensions ######################################################################
##################################################################################################
### Callbacks ####################################################################################
##################################################################################################
### Associations #################################################################################
##################################################################################################
### Validations ##################################################################################
##################################################################################################
### Scopes #######################################################################################
##################################################################################################
### Other ########################################################################################
##################################################################################################
### Class Methods ################################################################################
##################################################################################################
### Instance Methods #############################################################################
#########
protected
#########
#######
private
#######
end
@aroc
Copy link

aroc commented Dec 13, 2012

Money in the bank.

@smsohan
Copy link

smsohan commented Jun 8, 2013

I follow a similar pattern. Put my constants and includes at the top, as they may be used in the other parts.

@smsohan
Copy link

smsohan commented Jun 8, 2013

Some generally useful style guides here, if you haven't seen it. https://github.com/bbatsov/rails-style-guide

@joshuapinter
Copy link
Author

@smsohan Looking at it again I could see the Constants being more useful put at the top. Thanks.

@joshuapinter
Copy link
Author

Updated the model template so that Callbacks come before Associations. This is to prevent weird situations happening with dependent: :destroy where child objects are destroyed before hitting a before_destroy callback.

See the following for more details:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment