无法使用 Mongoid 修改数据

发布于 2024-11-09 17:22:48 字数 1336 浏览 0 评论 0原文

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

下面的示例成功输出了活动的设备类型,但它在保存方法上崩溃并显示以下错误消息:“undefined method `name' for 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

感谢您的帮助...

I can read my database using Mongoid, but cannot write to it.

This example below successfully outputs the activity's device type, but it crashes on the save method with this error message: "undefined method `name' for nil:NilClass"

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

Here are my class definitions:

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

Thanks for any help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2024-11-16 17:22:48

刚刚摆脱了 :inverse_of 语句,它现在可以工作了!

Just got rid of the :inverse_of statements and it works now!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文