2013-10-30 46 views
0

我想創建一個新的用戶羣多對多的關係。 我使用這些DataMapper的對象DataMapper隨着紅寶石sinatra多對多:低谷=> Reasource

module Core_authentication 

    class User 
    include DataMapper::Resource 

    property :id, Serial 
    property :username, String 
    property :password, BCryptHash 
    property :email, String 
    property :created_at, DateTime 
    property :updated_at, DateTime 

    #Creating join tables to link to group and person information 
    has n, :Person, :through => Resource 
    has n, :Group, :through => Resource 
    end 

    class Group 
    include DataMapper::Resource 

    property :id, Serial 
    property :name, String 

    #Another jointable link group to link to functions and classification levels 
    has n, :Function, :through => Resource 
    has n, :Classificationlevel, :through => Resource 
    end 

    class Person 
    include DataMapper::Resource 

    property :id, Serial 
    property :firstname, String 
    property :lastname, String 
    property :adress, String 
    property :postcode, String 
    property :telefoon, String 
    property :created_at, DateTime 
    property :updated_at, DateTime 

    end 

    class Function 
    include DataMapper::Resource 

    property :id, Serial 
    property :name, String 

    end 

    class Classificationlevel 
    include DataMapper::Resource 

    property :id, Serial 
    property :levelcode, String 
    property :name, String 

    end 

end 

這些代碼來創建和填寫表格

user = Core_authentication::User.create 
user.username = params['username'] 
user.password = params['password'] 
user.email = params['email'] 
#user.save 

group = Core_authentication::Group.first_or_create(:name => params['group']) 
group.name = params['group'] 
group.save 

user.Group << group 

但是,當我想嘗試它給了我這個錯誤

NoMethodError at /register 
undefined method `group' for #<Core_authentication::User:0x007f96cc2bf920> 

    file: App.rb 
    location: block in <class:App> 
    line: 46 

所以失敗關於我需要將這兩個表與兩個ID一起加入表的問題。 爲什麼它這樣做,笏是一個可能的解決方案? 是我把DataMapper對象放在模塊中的問題?

+0

不是'已N'參數應該是複數?例如。 '有n,:人* s *,:通過=>資源* s *'。另外,'belongs_to'裝飾品在哪裏? –

回答

0

修復了大量嘗試後的問題 通過使用has n, :model, through => Resource並將它放置在兩個模型類上都已修復。

這將創建一個連接表,其中包含兩個資源ID這裏的一個小例子。

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 

    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 

    end 

然後,只需創建2個對象然後通過

user.persons << person 

鏈接的對象,然後保存