0
votes

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...

1
I just figured out that if I create a brand new Activity it works... the problem happens when I update an existing one, just like in the above example.RooSoft
It seems that you should type "embedded_in :activity, :inverse_of => :trackpoints" in Trackpoint instead of "embedded_in :activity, :inverse_of => :trackpoint".Hck
You dont need field :user_id, :type => String in Activity model, you should replace "references_one :user" in Activity with "referenced_id :user" and replace "referenced_in :activity" with "references_one :activity" in User model.Hck
Thanks Hck, you my problem was effectively in the fields definitionRooSoft

1 Answers

0
votes

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