sites
sites

Reputation: 21815

Updating record with Mongoid in Rails

I am testing this from rails console:

Credential.last.token => nil
Credential.last.update_attribute :token, '123'   => true
Credential.last.token => nil

Here is my class:

class Credential
  include Mongoid::Document
  include Mongoid::Timestamps

  field :_id,              type: String
  field :user_id,          type: Integer
  field :code,             type: String
  field :provider,         type: String
  field :token,            type: String

end

What am I doing wrong?

Upvotes: 0

Views: 511

Answers (2)

sites
sites

Reputation: 21815

I had to put

attr_accessor :token, ...

Upvotes: 0

cpuguy83
cpuguy83

Reputation: 5993

If you have identity map enabled you'll need to wrap that in

Mongoid.unit_of_work { Credential.last.token }

Mongoid caches the queries. It is not a problem for web requests, but in the console you won't see the change unless you do it in the unit of work block, or restart the console (not just a reload)

Upvotes: 1

Related Questions