无法使用Mongoid修改数据

时间:2011-05-27 03:11:36

标签: ruby mongodb mongoid

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

下面的示例成功输出了活动的设备类型,但它在save方法中崩溃并显示以下错误消息:“未定义方法`名称为nil: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

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

1 个答案:

答案 0 :(得分:0)

刚刚摆脱了:inverse_of语句,现在就可以了!