开发者

Cannot modify data with Mongoid

开发者 https://www.devze.com 2023-03-09 03:20 出处:网络
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: \"undefin

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, :t开发者_JAVA百科ype => 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...


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

0

精彩评论

暂无评论...
验证码 换一张
取 消