我使用Rails进行Mongoid / MongoDB的新功能。我想在User和Project之间创建has_and_belongs_to_many asscoiation。
class Project
include Mongoid::Document
field :title, type: String
field :description, type: String
validates_presence_of :title, :description
has_and_belongs_to_many :users
end
class User
include Mongoid::Document
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, type: String, default: ""
field :encrypted_password, type: String, default: ""
## Recoverable
field :reset_password_token, type: String
field :reset_password_sent_at, type: Time
## Rememberable
field :remember_created_at, type: Time
## Trackable
field :sign_in_count, type: Integer, default: 0
field :current_sign_in_at, type: Time
field :last_sign_in_at, type: Time
field :current_sign_in_ip, type: String
field :last_sign_in_ip, type: String
## Confirmable
# field :confirmation_token, type: String
# field :confirmed_at, type: Time
# field :confirmation_sent_at, type: Time
# field :unconfirmed_email, type: String # Only if using reconfirmable
## Lockable
# field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
# field :unlock_token, type: String # Only if unlock strategy is :email or :both
# field :locked_at, type: Time
has_and_belongs_to_many :projects
end
但是当我进入控制台时,看起来它们没有连接。
{" _id" :ObjectId(" 564ddf63c705146553000001")," title" :"项目1 ","描述" :" ....." }
{" _id" :ObjectId(" 564dddc6c70514618c00000b")," email" : " user1@mail.com"," encrypted_password" : " $ 2A $ 10 $ xUAdz9V64IEH9oufE3kYauzZ / .xa5GBr / 0lapZDnSkwca40jH8 / I6&#34 ;, " sign_in_count" :1," reset_password_token" : 空值, " reset_password_sent_at" :null," last_sign_in_at" : ISODate(" 2015-11-19T14:33:43.293Z")," current_sign_in_at" : ISODate(" 2015-11-19T14:33:43.293Z")," last_sign_in_ip" :" :: 1", " current_sign_in_ip" :" :: 1" }
我希望Project的用户和user_ids都有project_ids。我做错了什么?
答案 0 :(得分:0)
我明白了!我应该在rails控制台中观看,而不是mongodb控制台。我仍然不明白,为什么这些游戏机会显示不同的结果。但是在rails控制台中,我有用于Project的project_ids和用于Project的user_ids。所以HABTM有效!