目前我已经建立了数据库模型并可以使用数据填充模型:
user.persons << person
group.functions << function
group.classificationlevels << clasfication
user.groups << group
这些是我目前使用的模型,用于获取彼此相关的数据:
module Core_authentication
class User
include DataMapper::Resource
property :id, Serial
property :username, String, :required => true, :unique => true
property :password, BCryptHash, :required => true
property :email, String, :format => :email_address, :required => true
property :created_at, DateTime
property :updated_at, DateTime
#Creating join tables to link to group and person information
has n, :persons, :through => Resource
has n, :groups, :through => Resource
def username= new_username
super new_username.downcase
end
end
class Group
include DataMapper::Resource
property :id, Serial
property :groupname, String, :required => true
#Another jointable link group to link to functions and classification levels
has n, :functions, :through => Resource
has n, :classificationlevels, :through => Resource
has n, :users, :through => Resource
def groupname= new_group
super new_group.downcase.capitalize!
end
end
class Person
include DataMapper::Resource
property :id, Serial
property :firstname, String
property :lastname, String, :required => true
property :adress, String
property :postcode, String, :length => 6, :required => true
property :telefoon, String
property :created_at, DateTime
property :updated_at, DateTime
has n, :users, :through => Resource
def firstname= new_firstname
super new_firstname.downcase.capitalize!
end
def lastname= new_lastname
super new_lastname
end
end
class Function
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :groups, :through => Resource
end
class Classificationlevel
include DataMapper::Resource
property :id, Serial
property :levelcode, Integer
property :name, String
has n, :groups, :through => Resource
end
end
我希望获得他们所属的用户组以及与每个组相关联的分类级别。
我尝试了多种方法,并在网上浏览过,但我无法找到关于如何做到这一点的明确解释,所以我无法让它发挥作用。
答案 0 :(得分:2)
documentation for Datamapper(“有,并且属于,许多(或多对多)”部分)有一些提示,这里是模型的简化示例:
require 'sqlite3'
require 'dm-core'
require 'dm-sqlite-adapter'
require 'dm-migrations'
DataMapper.setup(:default, 'sqlite3:m2m.sqlite3')
class User
include DataMapper::Resource
property :id, Serial
has n, :persons, :through => Resource # => PersonUser
has n, :groups, :through => Resource # => GroupPerson
end
class Group
include DataMapper::Resource
property :id, Serial
has n, :functions, :through => Resource # => FunctionGroup
has n, :classificationlevels, :through => Resource # => GroupClassificationlevel
has n, :users, :through => Resource # => GroupUser
end
class Person
include DataMapper::Resource
property :id, Serial
has n, :users, :through => Resource # => PersonUser
end
class Function
include DataMapper::Resource
property :id, Serial
has n, :groups, :through => Resource # => FunctionGroup
end
class Classificationlevel
include DataMapper::Resource
property :id, Serial
has n, :groups, :through => Resource # => GroupClassificationlevel
end
使用它们的示例(如果您将这两个片段放在一个文件中并运行它们,您可以看到输出):
DataMapper.finalize
DataMapper.auto_migrate!
user = User.create
group = Group.create
# link them by adding to the relationship
user.groups << group
user.save
p user.groups # => [#<Group @id=1>]