我正在尝试创建一个新的用户组多对多关系。我正在使用这些 dataMapper 对象
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 的表中时失败了。为什么它会这样做,并且 wat 是一个可能的解决方案?是我将 DataMapper 对象放在模块中的问题吗?