1

多対多の関係で新しいユーザー グループを作成しようとしています。これらの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 を持つテーブルに結合する必要がある時点で失敗します。なぜこれを行うのですか?可能な解決策は何ですか?DataMapper オブジェクトをモジュールに配置したことは問題ですか?

4

1 に答える 1

1

試行錯誤の末問題を修正 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

そして保存します

于 2013-12-02T08:42:33.670 に答える