Mongoid 6 alias field name

I'm upgrading a Rails 4.2 app with mongoid 5.2, but after the upgrade I find that options, client field names are not allowed.

This is the conclusion I arrived to after looking at mongoid code and seeing this exception:

NameError - undefined method options' for classScheduledReport': mongoid (6.1.1) lib/mongoid/errors/invalid_field.rb:44:in `origin'

Is there any way to define a field :foo that maps to :options or whatever field in the database?

This is my model definition:

class ScheduledReport
  include Mongoid::Document

  field :options, type: Hash, default: {}
end

Thanks in advance!

Upvotes: 0

Views: 148

Answers (1)

I didn't find any solution to map the field name, so I decided to rename the field with a migration as the affected collections are small:

collection = Mongoid.default_client[:scheduled_reports]
collection.find.each do |report|
  puts "#{report['options']} => #{report['configuration']}"
  if report['options']
    collection.update_one( { '_id' => report['_id'] }, { '$set' => { 'configuration' => report['options'] } } )
    object = collection.find('_id' => report['_id']).first
    puts "#{object['options']} => #{object['configuration']}"
  end
end

Upvotes: 0

Related Questions