Sql 带有ruby sinatra多对多的数据映射器:槽=>;资源
我正在尝试创建一个新的用户组多对多关系。 我正在使用这些dataMapper对象Sql 带有ruby sinatra多对多的数据映射器:槽=>;资源,sql,ruby,database,sinatra,datamapper,Sql,Ruby,Database,Sinatra,Datamapper,我正在尝试创建一个新的用户组多对多关系。 我正在使用这些dataMapper对象 module Core_authentication class User include DataMapper::Resource property :id, Serial property :username, String property :password, BCryptHash property :email, String property :c
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
user=Core\u身份验证::user.create
user.username=params['username']
user.password=params['password']
user.email=params['email']
#user.save
group=Core\u身份验证::group.first\u或\u create(:name=>params['group'])
group.name=params['group']
组。保存
经过多次尝试,user.Group修复了该问题
它是通过使用hasn,: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
然后只需创建两个对象,然后通过
user.persons << person
user.persons在多次尝试后修复了该问题
它是通过使用hasn,: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
然后只需创建两个对象,然后通过
user.persons << person
user.persons的参数不应该是复数吗?例如,有n,:person*s*,:through=>resource*s*
。另外,所属的装饰在哪里?难道有n个
参数不应该是复数吗?例如,有n,:person*s*,:through=>resource*s*
。另外,所属的装饰在哪里?