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對象放在模塊中的問題?
不是'已N'參數應該是複數?例如。 '有n,:人* s *,:通過=>資源* s *'。另外,'belongs_to'裝飾品在哪裏? –