2013-12-11 2 views
0

나는 데이터베이스 모델을 가지고 데이터 모델을 채울 수 있습니다 순간 :DataMapper의 many-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