我是 MongoDB 的新手,对 Rails 也有些陌生。我几乎完全在 JavaEE 中工作,并且正在尝试扩展。我发现了一个需要解决的问题:我的朋友想在线管理他们的 D&D 角色,但不喜欢当前的任何工具。听起来像是一个相对简单的应用程序。
我添加了登录的设计,并决定给 MongoDB 一个使用 Mongoid gem 的机会。经过一些阅读和一些教程后,我生成了我的应用程序,并且可以添加字符和登录/注册。因此,我的问题是:当我has_many :characters
在用户模型和角色模型中定义用户关系时,我在保存时不断在记录中belongs_to :user
获得返回值。nil
我相信我的设置正确,任何帮助将不胜感激。
user.rb(相关性被截断)
class User
include Mongoid::Document
include Mongoid::Timestamps
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, :type => String, :default => ""
field :encrypted_password, :type => String, :default => ""
validates_presence_of :email, :encrypted_password
index({ email: 1 }, { unique: true, background: true })
field :name, type: String
validates_presence_of :name
attr_accessible :name, :email, :password, :password_confirmation,
:remember_me, :created_at, :updated_at
## Mappings
has_many :characters
end
character.rb(相关性被截断)
class Character
include Mongoid::Document
field :character_name, type: String
field :level, type: Integer
field :experience, type: Integer
field :str, type: Integer
field :con, type: Integer
field :dex, type: Integer
field :int, type: Integer
field :wis, type: Integer
field :cha, type: Integer
field :editable, type: Boolean
belongs_to :cduser
end
控制台输出
1.9.3p448 :005 > Character.first
=> #<Character _id: 5201b4e76e9552c74a000002, character_name: "Test",
level: 1, experience: 1, str: 1, con: 1, dex: 1, int: 1, wis: 1, cha: 1,
editable: false, user_id: nil>
1.9.3p448 :006 > User.first
=> #<User _id: 5201b4aa6e95523fa4000001, created_at: 2013-08-07 02:44:58 UTC,
updated_at: 2013-08-07 02:44:58 UTC, email: "test@gmail.com", encrypted_password:
"$2a$10$SKlgfRDPYkloYP/Re6ZF2evBx1PEzRsE8JVPVhvdkLe3p0bSIcjX.",
reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil,
sign_in_count: 1, current_sign_in_at: 2013-08-07 02:44:58 UTC,
last_sign_in_at: 2013-08-07 02:4
4:58 UTC, current_sign_in_ip: "127.0.0.1",
last_sign_in_ip: "127.0.0.1", name: "Zach">