2013-12-11 36 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