Skip to content

Instantly share code, notes, and snippets.

@denniskuczynski
Created May 11, 2012 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denniskuczynski/2659789 to your computer and use it in GitHub Desktop.
Save denniskuczynski/2659789 to your computer and use it in GitHub Desktop.
Sharing common attributes and validations in Rails ActiveRecord to stay DRY
# Sharing common attributes and validations in Rails ActiveRecord to stay DRY
module CommonData
def self.included(clazz)
clazz.class_eval do
validates :duplicate_field, presence: true, length: { maximum: 255 }
attr_accessible :duplicate_field
end
end
end
class StagingRecord < ActiveRecord::Base
include CommonData
end
class RealRecord < ActiveRecord::Base
include CommonData
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment