无法使用 Mongoid 修改数据
我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚摆脱了 :inverse_of 语句,它现在可以工作了!
Just got rid of the :inverse_of statements and it works now!