Created
December 15, 2014 03:32
-
-
Save hopsoft/7a2afcb46ce14b5441ff to your computer and use it in GitHub Desktop.
Rails Modeling Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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