Reputation: 433
I am trying to create a new user group many to many relation. I am using these dataMapper Objects
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
and these code to create and fill the tables
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
But when i want try it gives me this error
NoMethodError at /register
undefined method `group' for #<Core_authentication::User:0x007f96cc2bf920>
file: App.rb
location: block in <class:App>
line: 46
So it fails on the point where i need to join the tables into a table with both ids. Why does it do this and wat is a possible solution? Is that i placed the DataMapper object in a module the problem?
Upvotes: 1
Views: 482
Reputation: 433
Fixed the problem after lots of try's
It was Fixed by using has n, :model, through => Resource
and placing it at both model classes.
This wil create a join table containing both resource ids here a small example.
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
Then just create the 2 objects then link the objects by
user.persons << person
and then save
Upvotes: 1