Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created December 15, 2014 03:32
Show Gist options
  • Save hopsoft/7a2afcb46ce14b5441ff to your computer and use it in GitHub Desktop.
Save hopsoft/7a2afcb46ce14b5441ff to your computer and use it in GitHub Desktop.
Rails Modeling Example
# Models
# app/models/user.rb
class User < ActiveRecord::Base
has_many :articles
has_many :videos
has_many :photos
end
# app/models/article.rb
class Article < ActiveRecord::Base
include IsContent
validates :text, presence: true
end
# app/models/photo.rb
class Photo < ActiveRecord::Base
include IsContent
include HasUrl
end
# app/models/video.rb
class Video < ActiveRecord::Base
include IsContent
include HasUrl
end
# Concerns
# app/models/concerns/is_content.rb
module IsContent
extend ActiveSupport::Concern
included do
belongs_to :user
end
end
# app/models/concerns/has_url.rb
module HasUrl
extend ActiveSupport::Concern
included do
validates :url, presence: true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment