2011-05-27 52 views
0

我可以使用Mongoid读取我的数据库,但无法写入数据库。无法使用Mongoid修改数据

这下面的例子成功输出活动的设备类型,但它崩溃的保存方法与此错误消息:“未定义的方法'名称”的零:NilClass

 
     activity = Activity.find('4d89568d4f21557eb10003fc') 
     puts activity.deviceType 
     activity.description = 'my description' 
     activity.save 

这里是我的班定义:

 
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 

感谢您的帮助......

+0

我刚刚发现,如果我创建一个全新的活动,它就会起作用......当我更新现有活动时,问题就会发生,就像上面的示例一样。 – RooSoft 2011-05-27 03:24:57

+0

您似乎应该在Trackpoint中输入“embedded_in:activity::inverse_of =>:trackpoints”,而不是“embedded_in:activity::inverse_of =>:trackpoint”。 – Hck 2011-05-27 08:39:29

+0

您不需要字段:user_id,:type =>在Activity模型中的字符串,您应该在Activity中将“references_one:user”替换为“referenced_id:user”,并将用户模型中的“referenced_in:activity”替换为“references_one:activity”。 – Hck 2011-05-27 08:44:30

回答

0

刚刚摆脱了:inverse_of语句,它禾现在!