Skip to content

Instantly share code, notes, and snippets.

@mamantoha
Created December 13, 2011 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamantoha/1472336 to your computer and use it in GitHub Desktop.
Save mamantoha/1472336 to your computer and use it in GitHub Desktop.
Models and associations
class Account
include DataMapper::Resource
property :id, Serial
property :name, String
#####
# 1 #
##### One-to-many-through
# Користувач є власником безлічі подій
has n, :authorships
has n, :events, :through => :authorships
###
#####
# 2 #
##### One-to-many-through
# Користувач є власником безлічі коментарів
has n, :commentators
has n, :comments, :through => :commentators
###
#####
# 4 #
##### Many-to-many-through
# Користувач може "лайкати" подію
has n, :likeables
has n, :likeds, :model => 'Event', :through => :likeables, :via => :event
###
end
class Event
include DataMapper::Resource
property :id, Serial
property :name, String
#####
# 1 #
##### One-to-many-through
# Подія має одного користувача
has n, :authorships
has 1, :owner, :model => 'Account', :through => :authorships, :via => :account
###
#####
# 3 #
##### One-to-many
# Подія має безліч коментарів
has n, :comments
#####
# 4 #
##### Many-to-many-through
# Подія має безліч лайків
has n, :likeables
has n, :likes, :model => 'Account', :through => :likeables, :via => :account
###
end
class Comment
include DataMapper::Resource
property :id, Serial
property :body, Text
#####
# 2 #
##### One-to-many-through
# Коментар має одного користувача
has n, :commentators
has 1, :owner, :model => 'Account', :through => :commentators, :via => :account
###
#####
# 3 #
##### One-to-many
# Коментар належить одній події
belongs_to :event
end
#####
# 1 #
##### One-to-many-through
class Authorship
include DataMapper::Resource
belongs_to :account, :key => true
belongs_to :event, :key => true
end
#####
# 2 #
##### One-to-many-through
class Commentator
include DataMapper::Resource
belongs_to :account, :key => true
belongs_to :comment, :key => true
end
#####
# 4 #
##### Many-to-many-through
class Likeable
include DataMapper::Resource
belongs_to :account, :key => true
belongs_to :event, :key => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment