Ruby 无法使用Mongoid修改数据

Ruby 无法使用Mongoid修改数据,ruby,mongodb,mongoid,Ruby,Mongodb,Mongoid,我可以使用Mongoid读取我的数据库,但不能写入 下面的示例成功输出活动的设备类型,但它在save方法上崩溃,并显示以下错误消息:nil:NilClass的未定义方法“name” 以下是我的类定义: class User include Mongoid::Document field :name, :type => String field :identifier, :type => String field :email, :type => String

我可以使用Mongoid读取我的数据库,但不能写入

下面的示例成功输出活动的设备类型,但它在save方法上崩溃,并显示以下错误消息:nil:NilClass的未定义方法“name”

以下是我的类定义:

class User include Mongoid::Document field :name, :type => String field :identifier, :type => String field :email, :type => String referenced_in :activity end class Trackpoint include Mongoid::Document field :when, :type => DateTime field :latitude, :type => Float field :longitude, :type => Float field :distance, :type => Float field :altitude, :type => Float field :heartRate, :type => Integer embedded_in :activity, :inverse_of => :trackpoint end class Activity include Mongoid::Document field :startTime, :type => DateTime field :description, :type => String field :sport, :type => String field :deviceType, :type => String field :deviceId, :type => String field :deviceActivityId, :type => String field :isPublic, :type => Boolean field :user_id, :type => String embeds_many :trackpoints references_one :user end
感谢您的帮助……

刚刚摆脱了:反向的语句,现在就可以工作了

我刚刚发现,如果我创建一个全新的活动,它会起作用。。。当我更新一个现有的跟踪点时,问题就出现了,就像上面的例子一样。似乎您应该在Trackpoint中键入embedded_in:activity,:inverse_of=>:trackpoints,而不是在Trackpoint中键入embedded_in:activity,:inverse_of=>:Trackpoint。您不需要在活动模型中输入field:user\u id,:type=>String,您应该将活动中的引用\u-one:user替换为引用的\u-id:user,并将活动中的引用\u-one:Activity替换为用户模型中的引用\u-one:Activity。谢谢Hck,我的问题有效地解决在字段定义中 class User include Mongoid::Document field :name, :type => String field :identifier, :type => String field :email, :type => String referenced_in :activity end class Trackpoint include Mongoid::Document field :when, :type => DateTime field :latitude, :type => Float field :longitude, :type => Float field :distance, :type => Float field :altitude, :type => Float field :heartRate, :type => Integer embedded_in :activity, :inverse_of => :trackpoint end class Activity include Mongoid::Document field :startTime, :type => DateTime field :description, :type => String field :sport, :type => String field :deviceType, :type => String field :deviceId, :type => String field :deviceActivityId, :type => String field :isPublic, :type => Boolean field :user_id, :type => String embeds_many :trackpoints references_one :user end