2013-12-11 31 views
0

目前我有数据库模型并可以填充数据模型:DataMapper的许多-to-many关联

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 

我想获得他们所属的用户组,以及与每个组关联的分类级别。

我尝试了多种方式来做到这一点,并且在网络上四处看了一下,但是我无法找到一个关于如何做到这一点的明确解释,所以我无法得到它的工作。

回答

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>] 
+0

谢谢,我终于明白了!但是,如果我在一个模块命名空间内工作,它的工作方式是什么? –

+0

对象应该是相同的,你只需要在模块名称的前面。 – phillbaker